Need help on "A Native Collection has not been disposed, resulting in a memory leak"
I've been working on an app for quite a while and never got this error until last week or so (after updating to 2021.1.2f1?) - and now it comes up all the time :-/
I don't know if this is related, but also since around last week, the contents of any vertical layout group in my app now sometimes suddenly becomes invisible or wiped. This happens only occasionally and so far only on the Android and iOS builds, not when running the app in the editor, which makes it tricky to debug.
After finally figuring out how to enable full stacktrace, I got a bit stuck on how to use the trace information (see below) to solve the problem. The information doesn't contain a reference to any code that i wrote myself, so I really don't know what to do with this. Any pointers on how to continue would be appreciated.
Thanks!
> A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
> Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator)
> UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[])
> UnityEngine.Networking.UnityWebRequest:SetupPost(UnityWebRequest, WWWForm)
> UnityEngine.Networking.UnityWebRequest:Post(String, WWWForm)
> System.Reflection.MonoMethod:InternalInvoke(Object, Object[], Exception&)
> System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)
> System.Reflection.MethodBase:Invoke(Object, Object[])
> Google.PortableWebRequest:StartRequest(HttpMethod, String, IDictionary`2, WWWForm) (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/PortableWebRequest.cs:481)
> Google.c_AnonStorey6:<>mC() (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/PortableWebRequest.cs:443)
> Google.RunOnMainThread:ExecuteNext() (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/RunOnMainThread.cs:486)
> Google.RunOnMainThread:m_12() (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/RunOnMainThread.cs:536)
> Google.RunOnMainThread:RunAction(Action) (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/RunOnMainThread.cs:343)
> Google.RunOnMainThread:ExecuteAllUnnested(Boolean) (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/RunOnMainThread.cs:530)
> Google.RunOnMainThread:ExecuteAll() (at /Users/chkuang/Workspace/Git/unity-jar-resolver/source/VersionHandlerImpl/src/RunOnMainThread.cs:512)
> UnityEditor.EditorApplication:Internal_CallUpdateFunctions()
Answer by stream_in_space_ · May 13, 2021 at 02:28 PM
It easy to fix. Just put your web request to a using construction. https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Networking.UnityWebRequest.Dispose.html
Thanks! I have a lot of unitywebrequests - is it important to put them all in this construction wrapper? (And if it is really important, i suggest that Unity does this automatically in 2021.2.X and onwards ^.^)
If you want to avoid a memory leak it should be important )
@stream_in_space_ Could you elaborate a lit more. Where do I find the web request. My game is crashing as soon as I launch it. Is it because of this issue? because I used admediation with unity and inMobi plugin and it works fine in unity, but when I used the updated version of that from play Store on my mobile it is crashing as soon as I launch
Answer by Yumby · Apr 23, 2021 at 11:27 PM
We are seeing this in 2021.1.3f1 as well.
I have been trying to fix this same issue in our code but have only been partially successful. I’m now calling Dispose explicitly on the UploadHandler after the UnityWebRequest response has been returned but it still intermittently (but not always) reports this leak.
I did not have to call Dispose in 2019 or 2020 and according to the docs, UploadHandler Dispose should be called be default when the UnityWebRequest is disposed.
This appears to be a race condition or logic bug of some kind in the Unity platform code. Is there source code available to review for any of these classes?
Answer by Chethan007 · May 30, 2021 at 08:45 AM
Yeah Even I am facing the same issue in 2021.1.7f1 personal edition too. what is the solution? My game is crashing in the beginning only