andywiecko / BurstTriangulator

2d Delaunay triangulation with mesh refinement for Unity with Burst compiler

Home Page:https://andywiecko.github.io/BurstTriangulator/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unity finds leaks from BurstTriangulator on domain reload when using RefineMesh

sinoth opened this issue · comments

commented

When RefineMesh = true, upon a domain reload Unity reports a leak. I'm new to Burst so I'm not sure how dire this is, but it only happens when RefineMesh = true and it seems to be a problem internal to the library.

Here is the call stack:

Leak Detected : Persistent allocates 257 individual allocations.
Found 257 leak(s) from callstack:
0x00007ff8b494e4b6 (fa990e2877010e42000f0e031e172c0) fa76a13e753195d7245d3caa5acc1551
0x00007ff71cc93ce2 (Unity) ExecuteJob
0x00007ff71cc950cf (Unity) ForwardJobToManaged
0x00007ff71cc90faa (Unity) ujob_execute_job
0x00007ff71cc928f9 (Unity) ujob_wait_for
0x00007ff71cc8cf27 (Unity) CompleteFenceInternal
0x00007ff71c08686f (Unity) JobHandle_CUSTOM_ScheduleBatchedJobsAndComplete
0x0000028059f6fdaa (Mono JIT Code) (wrapper managed-to-native) Unity.Jobs.JobHandle:ScheduleBatchedJobsAndComplete (Unity.Jobs.JobHandle&)
0x0000028059f2bf0b (Mono JIT Code) Unity.Jobs.JobHandle:Complete ()
0x0000028088603983 (Mono JIT Code) andywiecko.BurstTriangulator.Triangulator:Run () (at ./Library/PackageCache/com.andywiecko.burst.triangulator@2.4.0/Runtime/Triangulator.cs:240)
0x00000280885e8c3b (Mono JIT Code) IrregularGrid:TestTriangulator () (at E:/UnityProjects/./Assets/Scripts/IrregularGrid.cs:177)
0x0000028082749aeb (Mono JIT Code) IrregularGridEditor:OnInspectorGUI () (at E:/UnityProjects/./Assets/Scripts/IrregularGrid.cs:205)
0x000002808271f204 (Mono JIT Code) UnityEditor.UIElements.InspectorElement/<>c__DisplayClass72_0:<CreateInspectorElementUsingIMGUI>b__0 ()
0x0000028059e709ff (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoOnGUI (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,bool,UnityEngine.Rect,System.Action,bool)
0x000002805a1f466b (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:HandleIMGUIEvent (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,System.Action,bool)
0x000002805a25792b (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:HandleIMGUIEvent (UnityEngine.Event,System.Action,bool)
0x000002805a257783 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:HandleIMGUIEvent (UnityEngine.Event,bool)
0x000002808859873b (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:SendEventToIMGUIRaw (UnityEngine.UIElements.EventBase,bool,bool)
0x0000028088595793 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:SendEventToIMGUI (UnityEngine.UIElements.EventBase,bool,bool)

Reproducible with this code:

    public void TestTriangulator()
    {
        using var positions = new NativeArray<float2>(new float2[]
        {
            new(0, 0),
            new(1, 0),
            new(1, 1),
            new(0, 1)
        }, Allocator.Persistent);

        using var triangulator = new Triangulator(1024, Allocator.Persistent)
        {
            Input = { Positions = positions },
            Settings = { RefineMesh = true }
        };

        triangulator.Run();
    }

After running the above function, trigger a domain reload by either changing some code or manually triggering like this:

        [MenuItem("Tools/Force Domain Reload")]
        public static void ForceDomainReload()
        {
            EditorUtility.RequestScriptReload();
        }
commented

I've narrowed down the offending code to a single line:

using var _trianglesQueue = trianglesQueue = new NativeQueue<int>(Allocator.Temp);

This may be a leak with NativeQueue itself, as was seen in the past. Maybe someone can reason through this better than I can! I tried adjusting the use of the NativeQueue to get rid of the leak to no avail.

edit: Even if the RefineMeshJob::Execute method is lobotomized down to this:

            public void Execute()
            {
                using var _trianglesQueue = new NativeQueue<int>(Allocator.Temp);
            }

The leak still happens. So this seems to either be a benign leak due to running Burst jobs in the editor and then domain reloading, or a problem with NativeQueue itself. Interestingly running the job multiple times does not increase the leak count.

Hi @sinoth,
Thank you for your interest and contribution to the BurstTriangulation project! As far as I know, the 'Leak' message is an internal bug of the Unity.Collections/Unity.Burst package. I have just added a 'Known Issues' section in the README.md and mentioned this particular issue.

Best,
Andrzej