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:<>m__C() (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()

Still occurring in 2021.1.4f1

It easy to fix. Just put your web request to a using construction.

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?

I’ve got the same issue after upgrading to the 2021.1.15f1. No idea what causing it.

Getting this error for no reason in 2021.2.7f1 and .8
In Editor. Not my code.

And still in updated Unity 2021.3.1f1 LTS

I get the same error in 2021.2.f71, even with the using block.

2021.1.7f1 too-

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

https://forum.unity.com/threads/a-native-collection-has-not-been-disposed-resulting-in-a-memory-leak.1136068/

It seems UnityWebRequest has some special requirements for disposing. The linked answer fixed it for me.

I got this problem in “Unity 2020 version”
when i use WWW requests

WWW www = new(url, request.BytesData, request.headers);

i just added Using and it fixed thiss issue

using (WWW www = new(url, request.BytesData, request.headers)){}