A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.

In working on saving to file the NativeArrays used to fetch BlendShapeCoefficients in ARFoundation, I encountered the error above.

How does one enable the type of full stack trace this error requires to decipher where things went wrong?

  • I tried the full stack checkboxes on the bottom of Player Settings, but it still does not reveal further details.
  • I have tried to .Dispose() both upon beginning a new recording and on saving. Calling .Dispose led to another error InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
  • I also tried using brackets [DeallocateOnJobCompletion] above the NativeArray variable declarations (still occurs)
  • \$\begingroup\$ If you'd like help debugging the code that causes this error, I'd recommend editing your question to include a Minimal Complete Verifiable Example of that code. \$\endgroup\$ – DMGregory Oct 2 '20 at 8:48
  • 1
    \$\begingroup\$ As for enabling the full stack trace: I'm doing this out of my head, so I might be partially wrong, also this is based on how it worked in Unity 2019, in 2020 some might have moved to project settings by now, not sure. That setting should be under the top bar menu of 'jobs' (Next to File, Edit, Assets, that menu). However, this 'jobs' menu item (or the 'enable full stack trace' sub menu item) doesn't exist without the correct packages. I believe I had to get the 'jobs' package for it to exist, but might even need the 'Entities' package, not sure... \$\endgroup\$ – troien Oct 2 '20 at 9:23
  • \$\begingroup\$ i wish this were better documented @troien \$\endgroup\$ – ina Oct 2 '20 at 20:38

How does one enable the type of full stack trace this error requires to decipher where things went wrong?

Jobs > Leak Detection > Full Stack Traces

When you do this, instead of getting "just" an error that there was a memory leak, it will also tell you on which line the thing that's leaking was allocated. This can help you determine which script is creating the memory leak and which part of that script is leaking.

As for when you should dispose - you do it when you're done with the thing. Part of when you're "done" with it will depend on what Allocator you're using; Temp, Persistent, etc.

enter image description here

  • \$\begingroup\$ I didn't see that menu in my unity editor. \$\endgroup\$ – Xwtek Jun 4 at 1:01
  • \$\begingroup\$ @Xwtek - What version of Unity are you using? I posted this on February 23rd, and probably would have been using 2020.1 at the time; I'm using 2021.1 now and it's exactly the same. Also, please verify that you have the Jobs package installed: you have to go to "Window" then "Package Manager" then click the + in the upper-left of the package manager and use "Use Package By Name" then enter the name of the Jobs package com.unity.jobs \$\endgroup\$ – Chuck Jun 4 at 3:03
  • \$\begingroup\$ I used unity 2020.3.4f1 and I tried to click the plus sign, but it there is no Use Package By Name. I used Unity Jobs extensively in project, so I'm pretty much sure that I had com.unity.jobs installed. \$\endgroup\$ – Xwtek Jun 4 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.