[Solved] A Native Collection has not been disposed, resulting in a memory leak.

So I have receiving none critical error at OnCreateManager () after NativeArray allocation.

A Native Collection has not been disposed, resulting in a memory leak C:\User\ …

None critical, as code runs.

To simplyfy

// for now
static public List <Nodes> octrees = new List <Nodes> () ;

public struct Nodes
{
    public int lastNodeIndex ;
    public NativeArray <Bounds> bounds ;
    ...
}

protected override void OnCreateManager (int capacity)
{
NodesData octree = new NodesData () ;
octree.bounds = new NativeArray<Bounds> ( 100, Allocator.Persistent ) ;
...
// for now storing in the list
octrees.Add ( octree ) ;

// If I try add dispose, it throws error (see spoiler for full description)
// InvalidOperationException: The native container has been declared as [WriteOnly] in the job, but you are reading from it.
octree.a_bounds.Dispose () ;

}

InvalidOperationException: The native container has been declared as [WriteOnly] in the job, but you are reading from it.
Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x1c7831c0 + 0x00052> in :0
Unity.Collections.NativeArray1[T].CheckElementReadAccess (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:113) Unity.Collections.NativeArray1[T].get_Item (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:134)
ECS.Octree.Point.BoundingOctreeAddNodeSystem._Add2RootNode (System.Int32 i_octreeIndex, System.Int32 i_nodeIndex, ECS.Octree.EntityInstance entityInstance, ECS.Octree.Bounds entityBounds) (at Assets/Project/Scripts/Octree/Systems/BoundingOctreeAddNodeSystem.cs:455)
ECS.Octree.Point.BoundingOctreeAddNodeSystem._AddEntity (System.Int32 i_octreeIndex, ECS.Octree.EntityInstance entityInstance) (at Assets/Project/Scripts/Octree/Systems/BoundingOctreeAddNodeSystem.cs:418)
ECS.Octree.Point.BoundingOctreeAddNodeSystem+AddNode.Execute () (at Assets/Project/Scripts/Octree/Systems/BoundingOctreeAddNodeSystem.cs:268)
Unity.Jobs.IJobExtensions+JobStruct`1[T].Execute (T& data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at C:/buildslave/unity/build/Runtime/Jobs/Managed/IJob.cs:30)

Without dispose it works. However it throws errors at initialization, then let code run.
What I am missing?
[ReadOnly] ?

Is the List an issue?

The dispose call has to be done when you close the application or have no more use for the bounds array in the octrees node list.

if you dispose it right away you will get errors whenever you try to use the NodesData since the array is disposed.

3 Likes

Ah …
Silly me. Thx lot.

Added

protected override void OnDestroyManager ( )
{
.. dispose calls
}

This solves the issue.
So error was actually from previous simulation. Makes sense.

3 Likes

I would like indicate one more thing upon that error message, since it came back, after migrating to latest Entities preview 11.

It is more feature request.

It would be nice, if such error …

A Native Collection has not been disposed, resulting in a memory leak. It was allocated at C:\Users.…\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.collections@0.0.9-preview.3\Unity.Collections\NativeList.cs:24.

… could point into a script, where the error is generated. I got multiple scripts in project with native arrays and not sure, where the error is called from. Had no errors before upgrade, so now I need check manually every possible location, where dispose may be missing.

I have it on my todo list to add an option to track the callstack of these allocations. That should make them easy to track down. I should be able to get to this soon!

6 Likes

Most of them have some hints (# line of code) to allow track them down. Or even double click works.
But still having from to time, these which are hard to track down.

Anyway, looking forward to an update.
Thx

Hi Simon,

Do you know if there is any progress on this feature?

Thanks!

Pieter

With latest Entities 0.0.12 preview 20, error shows now the system *.cs file name and the job line.
This is big help already. Thx @simonm_unity
However, error do not indicates, which NativeArray may cause the problem.
So if you have more than one array, this may become tedious to find source of issue.

Support for full stack traces is done (not by me) and should make it into 2019.1.
So hopefully you should be able to use this soon.

6 Likes

How do I enable the full stack trace on 2019.2.0a4? I get a bunch of "
A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details." without any traces, just that line. Already tried enabling it from the console menu and player settings, even disabled burst but still the same.

2 Likes

Any follow up from the above? I’m on 2019.1.0b4, can’t see a way to turn it on… setting somewhere?

Main Menu > Jobs > Leak Detection > Full Stack Traces (Expensive)

8 Likes

I don’t seem to have that menu in 2019.1.0b4. I tried installing the jobs package (0.0.7) but it doesn’t seem to compile on 2019.1.

Try 2019.1.0b5.

1 Like

That menu option comes with the Jobs package. Good luck finding the right combination of versions to make it compile. Wiping the shader and package cache does help sometimes.

1 Like

Thanks; I was looking for this!

Taken from another thread:

if you are looking for the option and you only see Burst inside the Jobs menu, then you have to go to the package manager and install the Entities package, that has a lot of dependencies and will make the rest of the options visible (at least with 2019.4).

After that you will see the Leak Detection options and the jobs debugger.

I don’t think this is necessary anymore. With the latest burst, jobs and collections packages I get the options

I get [quote]
A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.
[/quote] I believe this is related to PlayFab.

1 Like

Yep. You’re right. It’s working now.