question

Richard Berger avatar image
Richard Berger asked

I'm getting the following error in PlayFab in Unity. Does anyone have any idea how to fix it? It's beyond me.

unity3d
1 comment
10 |1200

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

Richard Berger avatar image Richard Berger commented ·

Here's the error message:

A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.

I get it on a regular basis, sometimes when the code is running, sometimes when I'm just in the editor. It doesn't seem to be breaking anything but I'd like to be able to fix it.

0 Likes 0 ·
Ivan Cai avatar image
Ivan Cai answered

This issue seems to belong to the business category of unity, not playfab. You need to navigate to [Jobs]->[Leak Detection]->[Full Stack Traces], select it and restart unity.Then the error content in the console will be show that the memory leak occurred in the data retention location. After knowing the location of the leak, you just put the variable in Dispose, but it is recommended to enclose it in the using statement.

10 |1200

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

Ralv Wohlgethan avatar image
Ralv Wohlgethan answered

@Richard Berger You might need to actually include the jobs package (manually, maybe) in your manifest.json file:

"dependencies": {
  ...  
  "com.unity.jobs": "0.8.0-preview.23",
  ...
}

Depending on who reads this post and when, the actual package version may of course differ, but you should be able to update from the editor (PackageManager UI) after modifying your manifest.json (unless Unity decides to drop the package or integrate it elsewhere, of course, but then this answer will be outdated anyway...).

10 |1200

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

Ralv Wohlgethan avatar image
Ralv Wohlgethan answered

Actually (if it gets published) you can ignore my previous answer. While the number of memory leaks has seemingly decreased with the integration of the latest jobs package, they do still occur and seem to originate from the PlayFabUnityHttp class. At least this is what I'd conclude from the stacktrace:

A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator) (at /Users/bokken/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:69)
UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[]) (at /Users/bokken/buildslave/unity/build/Modules/UnityWebRequest/Public/UploadHandler/UploadHandler.bindings.cs:98)
PlayFab.Internal.<Post>d__12:MoveNext() (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:129)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/MonoBehaviour.bindings.cs:91)
PlayFab.Internal.PlayFabUnityHttp:MakeApiCall(Object) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:119)
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:ReportDeviceInfo(DeviceInfoRequest, Action`1, Action`1, Object, Dictionary`2) (at Assets/PlayFabSDK/Client/PlayFabClientAPI.cs:1751)
PlayFab.Internal.PlayFabDeviceUtil:SendDeviceInfoToPlayFab(PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Client/PlayFabDeviceUtil.cs:54)
PlayFab.Internal.PlayFabDeviceUtil:_OnPlayFabLogin(UserSettings, String, String, String, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Client/PlayFabDeviceUtil.cs:127)
PlayFab.Internal.PlayFabDeviceUtil:OnPlayFabLogin(PlayFabResultCommon, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Client/PlayFabDeviceUtil.cs:102)
PlayFab.Internal.PlayFabUnityHttp:OnResponse(String, CallRequestContainer) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:234)
PlayFab.Internal.<Post>d__12:MoveNext() (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:198)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

Don't mind the "/Users/bokken/..." paths, this is Unity's build machine, so no need to worry.

In our own implementations with UnityWebRequest we always use "using" blocks. This ensures that the request is always disposed of properly. I'm not sure why this isn't the case in PlayFabUnityHttp, but a first look seems to imply that it is indeed possible to create a memory leak when the manual call of

www.Dispose()

at the very end isn't reached... Interesting enough, the code contradicts @Ivan Cai's proposal of doing exactly this (using "using" blocks)...

Another error source seems to be in PlayFabEditorHttp:

A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator) (at /Users/bokken/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:69)
UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[]) (at /Users/bokken/buildslave/unity/build/Modules/UnityWebRequest/Public/UploadHandler/UploadHandler.bindings.cs:98)
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:198)
PlayFab.PfEditor.PlayFabEditor:OnEnable() (at Assets/PlayFabEditorExtensions/Editor/PlayFabEditor.cs:45)
PlayFab.PfEditor.PlayFabEditor:OnFocus() (at Assets/PlayFabEditorExtensions/Editor/PlayFabEditor.cs:62)
UnityEditor.HostView:RegisterSelectedPane(Boolean) (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:502)
UnityEditor.HostView:OnEnable() (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:154)
UnityEditor.DockArea:OnEnable() (at /Users/bokken/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:174)

In this case, at first glance, I can't find neither a using block nor any Dispose() call. Hope this helps...


Playfab SDK version: 2.107.210427(shown as latest release as of 2021-05-08)

10 |1200

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

Richard Berger avatar image
Richard Berger answered

Here's the stack trace that I did manage to then get - maybe this helps...

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:129)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
PlayFab.Internal.PlayFabUnityHttp:MakeApiCall(Object) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:119)
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()
1 comment
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 ♦ commented ·

Hi, we have noticed the memory leak issue in 2021 Unity Engine. However, to manually fix this issue, it may need plenty of work to put web request to a using construction, according to: Need help on "A Native Collection has not been disposed, resulting in a memory leak" - Unity Answers.

For now, you can switch to the LTS version of Unity to avoid this warning, otherwise, please wait for the updates of Unity or PlayFab Unity SDK.

0 Likes 0 ·
Chethan V avatar image
Chethan V answered

I closed Unity,deleted the Library folder and restarted Unity. Unity automatically imported the necessary files and the error went away. Hope this helps someone

10 |1200

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

CHO YOUNGMIN avatar image
CHO YOUNGMIN answered

Actually it's easy to solve.

This is a PlayFabSharedSettings ' directory problem.

When you import Playfab SDK, basically PlayFabSharedSettings is exist in [PlayFabSDK] - [Shared] - [Public] - [Resources].

But take a look the PlayFabSettings.GetSharedSettingsObjectPrivate() .

It load all PlayFabSharedSettings from "Resources" like :

var settingsList = Resources.LoadAll<PlayFabSharedSettings>("PlayFabSharedSettings");

return settingsList[0];.

So basically as all we know, Resources. ~~ access to [Asset] - [Resources].

Not [PlayFabSDK] - [Shared] - [Public] - [Resources].

So go [PlayFabSDK] - [Shared] - [Public] - [Resources], Copy PlayFabSharedSettings,

Paste to [Asset] - [Resources]. then it will works.

2 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.

toddhd avatar image toddhd commented ·

Thank you for this. This fixed the problem for me and only took a moment to do.

0 Likes 0 ·
chronocantway avatar image chronocantway commented ·

Awesome thank you Cho! Just to clarify and add for others.

-Go to your folder directory for your game/project

-Then go to Assets > PlayFabSDK > Shared > Public > Resources

-Copy the file "PlayFabSharedSettings.asset"

-Paste the file in your game/project directory Assets > Resources

Disclaimer: If you do not have a "Resources" folder, simply create a new folder, and name it "Resources" then paste the file into that folder.

Thanks again Cho!

0 Likes 0 ·

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.