Robmaister / SharpNav

Advanced Pathfinding for C#
sharpnav.com
Other
537 stars 129 forks source link

Static member thread safety: IndexOutOfRangeException in ClipPolygonToPlane #26

Closed AqlaSolutions closed 9 years ago

AqlaSolutions commented 9 years ago

The same old obj: https://www.dropbox.com/s/jymf3n0oqwfhidr/Fighting_scene_navmesh.obj?dl=0

Sometimes (perhaps in multithread environment) it throws: http://clip2net.com/clip/m203122/86185-clip-78kb.png?nocache=1

I'll try to investigate this further...

AqlaSolutions commented 9 years ago

Yep, it's definitely threading issue.

        var parameters = NavMeshGenerationSettings.Default;
        parameters.AgentHeight = settings.MaxHeight;
        parameters.AgentWidth = 1;
        parameters.MaxClimb = settings.MaxClimb;
        Triangle3[] tris = TriangleEnumerable.FromTriangle(level.GetTriangles(), 0, level.GetTriangles().Length).ToArray();
        Thread.MemoryBarrier();
        Enumerable.Range(1, 1000)
            .AsParallel()
            .Select(x => SharpNav.NavMesh.Generate(tris, parameters)).ToArray();
Robmaister commented 9 years ago

Yeah, without looking at the code I'm pretty sure I can pinpoint a Recast pull-request that added the breaking change. As triangles are getting voxelized, there was an optimization that stored intermediate variables in a static array. It should be somewhere in MathHelper.cs.

The solution would be to either pass the array in as a parameter or use the older method when thread-safety is desired. In the future I'd also like to parallelize the generation process anyways, since it's so easy with the Task Parallel Library.

I'm about to get on a flight, I'll make an attempt at fixing this on the plane.

Robmaister commented 9 years ago

Let me know if that commit fixes your issue, I'll leave the issue open for the time being.

AqlaSolutions commented 9 years ago

Yes, it works now. Thanks!