question

Manuel Rauber avatar image
Manuel Rauber asked

Possible Bug/Memory Leak in PlayFab Unity SDK when using PlayFabUnityHttp with Simple*Call

Hi,

I'm using Unity 2020.3.18f1 with PlayFab SDK Version 2.113.210830 to create a little iOS game.

I noticed that after a certain time, the iPhone reports that the app is using too much memory and thus will be closed.

Looking via XCode instruments and Unity profiler, I can see, that the native memory skyrockets after some time that leads to closing the app. But only Xcode reports that the memory skyrockets, but not the Unity profiler, so it has to be something native.

I tried to tackle the issues to find out, where it comes from. I figured out, that certain requests using PlayFab seem to start the memory leak, so I was taking a look into the code.

The "normal" SDK methods will use the MakeApiCall-method to communicate with the PlayFab-API: https://github.com/PlayFab/UnitySDK/blob/master/ExampleMacProject/Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs#L105

It starts a Coroutine called "Post".

At the very end of this Post-Method you can see, that the UnityWebRequests gets disposed: https://github.com/PlayFab/UnitySDK/blob/master/ExampleMacProject/Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs#L164

So far, so good.

But, in my game I'm using SimplePutCall to upload something to the server.

However, as you can see here: https://github.com/PlayFab/UnitySDK/blob/master/ExampleMacProject/Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs#L61

An instance of UnityWebRequest will be created, but you will NOT see any "request.Dispose()" at the end of the method.

If I add a request.Dispose() at the end, the memory leak seems to be resolved.

Could it be, that in the Unity SDK this request.Dispose() was forgotten, thus never releasing the native memory that is allocated by UnityWebRequest?

unity3d
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JayZuo avatar image
JayZuo answered

@Manuel Rauber As you've fixed it, could you please contribute it to the SDK by submitting a pull request? The Unity SDK is generated PlayFab/SDKGenerator and the source code is under SDKGenerator/targets/unity-v2/source/ExampleTestProject/Assets/PlayFabSDK at master PlayFab/SDKGenerator (github.com).

Thanks for your contribution!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image
Seth Du answered

Thanks for the feedback and the workaround solution. I will discuss with the team.

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Manuel Rauber avatar image Manuel Rauber commented ·

Any news about this? at least, if you can confirm, that this Dispose is missing?

Thanks!

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Manuel Rauber commented ·

Hi, can you share a sample or code snippets with us for the reproduction? Please remember to remove private information.

0 Likes 0 ·
Manuel Rauber avatar image Manuel Rauber Seth Du ♦ commented ·

Hi Seth,

well, it's simply using the API mentioned in my original post. Uploading a file to PlayFab as shown in your documentation pages.

After I've fixed it, as written in my post, I've not seen the memory sky rocketing anymore.

Still, if you take a look at the SDK code, you can clearly see, that the web request is never disposed.

0 Likes 0 ·
Show more comments
Daniel Edward Barabas avatar image
Daniel Edward Barabas answered

I was having the same error and your method fixed it for me, but I kept getting similar errors afterwards and traced them to the Post method in UnitySDK/PlayFabEditorHttp.cs at master PlayFab/UnitySDK GitHub. I just added Dispose() to all the Web Requests in the Post and PostDownload methods and it seems to have worked perfectly so far.

Ex:

private static IEnumerator Post(UnityWebRequest www, Action<string> callBack, Action<string> errorCallback)
        {
            if (www != null)
            {
                yield return www.SendWebRequest();


                if (!string.IsNullOrEmpty(www.error))
                    errorCallback(www.error);
                else
                    callBack(www.downloadHandler.text);

		// this Dispose call was not here initially
                www.Dispose();
            }
            else
            {
                UnityEngine.Debug.Log("UnityWebRequest was null");
                errorCallback("UnityWebRequest Object was null");
            }
        }
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.