Search Unity

Bug User Reporting SDK: A Native Collection has not been disposed, resulting in a memory leak

Discussion in 'Unity Cloud Diagnostics' started by Tudvari, Oct 18, 2022.

  1. Tudvari

    Tudvari

    Joined:
    Jun 3, 2014
    Posts:
    30
    Hi,

    I'm using Unity 2021.2.11f and I've just enabled Cloud Diagnostics and added the User Reporting SDK.
    Haven't changed a thing but when I start or stop play mode, this error occurs:
    These are the 340-341 lines of UserReportingScript
    Code (CSharp):
    1. string url = string.Format("https://userreporting.cloud.unity3d.com/api/userreporting/projects/{0}/ping", UnityUserReporting.CurrentClient.ProjectIdentifier);
    2. UnityUserReporting.CurrentClient.Platform.Post(url, "application/json", Encoding.UTF8.GetBytes("\"Ping\""), (upload, download) => { }, (result, bytes) => { });
    These are 376-377 lines of UnityUserReportingPlatform:

    Code (CSharp):
    1. UnityWebRequest webRequest = new UnityWebRequest(endpoint, "POST");
    2. webRequest.uploadHandler = new UploadHandlerRaw(content);
    Did I configured something badly? Or is this an issue with the package?

    Thanks in advance!
     
    Immu likes this.
  2. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    489
    The sdk will be released as a package manager package somewhere soon where this is fixed. We either stay with the old one or wait for the new one to be released.
     
    Tudvari likes this.
  3. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    Hello, any ETA about how 'soon' can we expect the package to appear ?
    Isn't it problematic that the current one to download is flawed with that memory leak ? How to revert to the 'old one' ?
    Thanks :)
     
  4. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    489
  5. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    What a disgrace -_-. Thanks for the info!
     
    MiTschMR likes this.
  6. alebascoEndless

    alebascoEndless

    Joined:
    May 16, 2022
    Posts:
    2
    Insert these two lines in UnityUserReportingPlatform line 379 and I believe it fixes this error.

    webRequest.disposeUploadHandlerOnDispose = true;
    webRequest.disposeDownloadHandlerOnDispose = true;

    Not sure why they don't have this on package manager yet, or why they haven't at least fixed the downloadable one.
     
  7. sajkowart

    sajkowart

    Joined:
    Oct 7, 2020
    Posts:
    19
    ^ Disposing on line 379 doesn't work. Since webrequest is cached in postOperation and later f's.
    Instead try this:
    Where you are initializing IUserReportingPlatform

    Assembly assembly = Assembly.GetExecutingAssembly();
    Type asyncUnityUserReportingPlatformType =
    assembly.GetType("Unity.Cloud.UserReporting.Plugin.Version2018_3.AsyncUnityUserReportingPlatform");
    if (asyncUnityUserReportingPlatformType != null)
    {
    object activatedObject = Activator.CreateInstance(asyncUnityUserReportingPlatformType);
    IUserReportingPlatform asyncUnityUserReportingPlatform = activatedObject as IUserReportingPlatform;
    if (asyncUnityUserReportingPlatform != null)
    {
    UnityUserReporting.Configure(asyncUnityUserReportingPlatform, this.GetConfiguration());
    configured = true;
    }
    }


    You can see you create an instance of this. Therefore you can also dispose of this. What I've done is to extend interface

    public interface IUserReportingPlatform :IDisposable

    It will give you error of those instances. In the dispose methods just set all the class fields to null.
    For example:
    In "UnityUserReportingPlatform.cs"

    public void Dispose()
    {
    this.postOperations = null;
    this.logMessages = null;
    this.profilerSamplers = null;
    this.screenshotOperations = null;
    this.screenshotStopwatch = null;
    this.taskOperations = null;
    }
     
  8. sajkowart

    sajkowart

    Joined:
    Oct 7, 2020
    Posts:
    19
    And call the cached Dispose()
     
  9. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    Hello, just to be clear, calling the dispose() is required at UnityUserReportingPlatform line 379, is that right ?
    In any case, thanks a lot for helping, you're very kind !
    I can't believe Unity team didn't yet backported a fix for such a huge leak in an active LTS like 2021.3 X/.

    I also seem to have to add a dispose method there (empty that is) in that script, it's legit right ?
    upload_2023-3-27_12-19-4.png
    as well as here:
    upload_2023-3-27_12-21-20.png
     
    Last edited: Mar 27, 2023
  10. sajkowart

    sajkowart

    Joined:
    Oct 7, 2020
    Posts:
    19
    It doesn't really matter if there is an empty method. Since it is required be implementing this interface now with dispose it needs to have it.

    After that you need to call the DIspose() from somewhere (probably from the place you started creating IUserReportingPlatform) when you don't need it anymore i.e. OnApplicationQuit()
     
    Immu likes this.