Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • Bugs /
avatar image
Question by johnscaban · Mar 30 at 06:21 PM · sdks

Memory Leaks in Unity SDK

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()
Comment

People who like this

0 Show 1
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image edwarddennekamp · Jun 22 at 07:45 PM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 4 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image johnscaban · Mar 31 at 01:38 PM 0
Share

Ok, but the first and third memory leaks are not from the PlayFabEditorExtensions.

avatar image Made Wang johnscaban · Apr 01 at 09:36 AM 0
Share

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.

avatar image johnscaban Made Wang · Apr 01 at 04:51 PM 0
Share

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.

Show more comments
avatar image

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
    }
}
Comment

People who like this

0 Show 3 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image johnscaban · Apr 07 at 01:05 PM 0
Share

Btw, the request.Send() is obsolete in newer unity versions. An equivalent is request.SendWebRequest().

avatar image Made Wang johnscaban · Apr 11 at 10:14 AM 0
Share

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.

avatar image johnscaban Made Wang · Apr 18 at 12:37 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    15 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    UE 4.18 Playfab crash when connecting fails 1 Answer

    UnityWebRequest does not use timeout 1 Answer

    UE4 continually crashing 1 Answer

    Encoding problem when starting the server (Session Cookie) 1 Answer

    Unity3D Editor Extensions NullReferenceException on install, can't start 1 Answer

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges