I get sometimes these memory leaks when using the Unity SDK.
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[]) PlayFab.Internal.<Post>d__12:MoveNext() (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:117) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) UnityEngine.MonoBehaviour:StartCoroutineManaged2(MonoBehaviour, IEnumerator) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) PlayFab.Internal.PlayFabUnityHttp:MakeApiCall(Object) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:107) PlayFab.Internal.PlayFabHttp:_MakeApiCall(String, String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, Boolean, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:228) PlayFab.Internal.PlayFabHttp:MakeApiCall(String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:133) PlayFab.PlayFabClientAPI:LoginWithCustomID(LoginWithCustomIDRequest, Action`1, Action`1, Object, Dictionary`2) (at Assets\PlayFabSDK\Client\PlayFabClientAPI.cs:1285) PlayFabManager:AnonymousLogin(Action`1, Action`1) (at Assets\Scripts\Game\PlayFabManager.cs:65) PlayFabManager:CheckForUser() (at Assets\Scripts\Game\PlayFabManager.cs:53) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
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[]) PlayFab.PfEditor.PlayFabEditorHttp:MakeApiCall(String, String, GetStudiosRequest, Action`1, Action`1) (at Assets\PlayFabEditorExtensions\Editor\Scripts\PlayFabEditorSDK\PlayFabEditorHttp.cs:74) PlayFab.PfEditor.PlayFabEditorApi:GetStudios(GetStudiosRequest, Action`1, Action`1) (at Assets\PlayFabEditorExtensions\Editor\Scripts\PlayFabEditorSDK\PlayFabEditorApi.cs:30) PlayFab.PfEditor.PlayFabEditorDataService:RefreshStudiosList(Boolean) (at Assets\PlayFabEditorExtensions\Editor\Scripts\Utils\PlayFabEditorDataService.cs:197) PlayFab.PfEditor.PlayFabEditor:OnEnable() (at Assets\PlayFabEditorExtensions\Editor\PlayFabEditor.cs:45)
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[]) PlayFab.Internal.<Post>d__12:MoveNext() (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:117) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) UnityEngine.MonoBehaviour:StartCoroutineManaged2(MonoBehaviour, IEnumerator) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) PlayFab.Internal.PlayFabUnityHttp:MakeApiCall(Object) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:107) PlayFab.Internal.PlayFabHttp:_MakeApiCall(String, String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, Boolean, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:228) PlayFab.Internal.PlayFabHttp:MakeApiCall(String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:133) PlayFab.PlayFabEventsInstanceAPI:WriteEvents(WriteEventsRequest, Action`1, Action`1, Object, Dictionary`2) (at Assets\PlayFabSDK\Events\PlayFabEventsInstanceAPI.cs:63) PlayFab.Public.ScreenTimeTracker:Send() (at Assets\PlayFabSDK\Entity\ScreenTimeTracker.cs:181) PlayFab.Public.ScreenTimeTracker:OnApplicationFocus(Boolean) (at Assets\PlayFabSDK\Entity\ScreenTimeTracker.cs:156) PlayFab.Internal.PlayFabHttp:OnApplicationFocus(Boolean) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:332) UnityEditorInternal.InternalEditorUtility:OnGameViewFocus(Boolean) UnityEditor.PlayModeView:SetFocus(Boolean) UnityEditor.GameView:OnLostFocus() UnityEditor.HostView:OnLostFocus()
Still happens in Unity 2021.3.4f1 with Playfab SDK 2.138.220621
NOTE, I have Playfab disabled in my game and it's still throwing this error.
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[]) PlayFab.PfEditor.PlayFabEditorHttp:MakeApiCall(String, String, GetStudiosRequest, Action`1, Action`1) (at Assets\PlayFabEditorExtensions\Editor\Scripts\PlayFabEditorSDK\PlayFabEditorHttp.cs:74) PlayFab.PfEditor.PlayFabEditorApi:GetStudios(GetStudiosRequest, Action`1, Action`1) (at Assets\PlayFabEditorExtensions\Editor\Scripts\PlayFabEditorSDK\PlayFabEditorApi.cs:30) PlayFab.PfEditor.PlayFabEditorDataService:RefreshStudiosList(Boolean) (at Assets\PlayFabEditorExtensions\Editor\Scripts\Utils\PlayFabEditorDataService.cs:197) PlayFab.PfEditor.PlayFabEditor:OnEnable() (at Assets\PlayFabEditorExtensions\Editor\PlayFabEditor.cs:45)
Answer by Made Wang · Mar 31 at 10:12 AM
This is a known issue with PlayFabEditorExtensions, you can refer to the method in Possible Bug/Memory Leak in PlayFab Unity SDK when using PlayFabUnityHttp with Simple*Call - Playfab Community and Need help on "A Native Collection has not been disposed, resulting in a memory leak" - Unity Answers to fix it manually.
In addition, PlayFabEditorExtensions is not necessary, and deleting it will not affect your use of the PlayFab SDK. You can refer to Installing the PlayFab SDK for Unity - PlayFab | Microsoft Docs to manually configure the PlayFab SDK.
Ok, but the first and third memory leaks are not from the PlayFabEditorExtensions.
Are you using the latest version of PlayFab SDK? If you still have this issue in the latest version, please provide the Unity version and related code so that we can reproduce it.
Yes I'm using the latest SDK version.
I'm using Unity 2021.2.16. The first and third memory leaks happen sometimes, more frequently when being recently disconnected from the internet. The first memory leak involves this code:
void Start() { AnonymousLogin(result => { DisplayName = result.InfoResultPayload.PlayerProfile.DisplayName; MainMenuUI.Singleton.LoggedIn(); }, error => { MainMenuUI.Singleton.LoginFailed(error.GenerateErrorReport()); }); } void AnonymousLogin(Action<LoginResult> resultCallback, Action<PlayFabError> errorCallback) { PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest { CustomId = _CustomId, CreateAccount = false, InfoRequestParameters = new GetPlayerCombinedInfoRequestParams { GetPlayerProfile = true, }, }, result => { SessionTicket = result.SessionTicket; resultCallback.Invoke(result); }, error => { Debug.Log(error.GenerateErrorReport()); errorCallback.Invoke(error); }); }
The third memory leak happens internally within the SDK. The code of mine involves that leak.
Answer by johnscaban · Apr 07 at 01:00 PM
The memory leaks keep happening. It has to do with the improper handling of the UnityWebRequest.
For feedback, a good solution for this is to use the using statement. The using statement ensures that all resources get cleaned up even if an exception occurs. An example would be:
public IEnumerator Post() { using (UnityWebRequest request = UnityWebRequest.Get("https://endpoint_url.com")) { // Any code before sending the request yield return request.Send(); // Rest of code here } }
Btw, the request.Send() is obsolete in newer unity versions. An equivalent is request.SendWebRequest().
request.SendWebRequest() has been used in the latest version, refer toUnitySDK/PlayFabUnityHttp.cs at master PlayFab/UnitySDK (github.com). You can download the latest Unity SDK fromhttps://aka.ms/playfabunitysdkdownload.
I've given up in the PlayFab SDK. The error still happened on the Unity LTS version. So I'm not going to use PlayFab anymore. Thanks for your help.