2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray1:.ctor(Byte[], Allocator)
UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[])
UnityEngine.Networking.UnityWebRequest:SetupPost(UnityWebRequest, WWWForm)
The full text of the error is as above
See the solution first
using (var request = UnityWebRequest.Post(serverURL, "POST"))
using (var uh = new UploadHandlerRaw(bodyRaw))
{
//request.disposeUploadHandlerOnDispose = true;
//request.disposeDownloadHandlerOnDispose = true;
//request.disposeCertificateHandlerOnDispose = true;
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
yield return request.SendWebRequest();
}
This can be solved by using two using sets
I checked a lot of information on the Internet, and many people said that the memory leak was caused by the uploaderhandler they created not being released.
Many people say that using
//request.disposeUploadHandlerOnDispose = true;
//request.disposeDownloadHandlerOnDispose = true;
//request.disposeCertificateHandlerOnDispose = true;
It's fine to set these three, but I tried it and the effect was not ideal. It may be due to problems with Unity itself, or it may have been changed.
Then I thought, can I release the uploadhandler manually? I tried it.
request.uploadHandler.Dispose();
The effect is not ideal. I even heard from an old man on the Internet that using the constructor to create
byte[] bodyRaw = Encoding.UTF8.GetBytes(send_data);
webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
The effect is still not ideal.
Then in
A Native Collection has not been disposed, resulting in a memory leak. - Unity Forum
I found the solution at the bottom of this page. Thanks to this guy. It solved the problem that has been bothering me for a long time.
I found this forum on another author's blog.
https://blog.csdn.net/Miner_W/article/details/129400923
Thanks to this brother too.