Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Why does Addressables.Release destroy the result from WaitForCompletion in Windows Build?

Discussion in 'Addressables' started by sauerkraut, Sep 3, 2022.

  1. sauerkraut

    sauerkraut

    Joined:
    Feb 21, 2014
    Posts:
    23
    I am using the recommended code structure (shown below) for loading Addressables with WaitForCompletion.

    Code (CSharp):
    1. void Start()
    2. {
    3.     //Basic use case of forcing a synchronous load of a GameObject
    4.     var op = Addressables.LoadAssetAsync<GameObject>("myGameObjectKey");
    5.     GameObject go = op.WaitForCompletion();
    6.  
    7.     //Do work...
    8.  
    9.     Addressables.Release(op);
    10. }
    11.  
    My specific implementation is below:

    Code (CSharp):
    1.     public AudioClip LoadAudioClip(string key)
    2.     {
    3.         var op = Addressables.LoadAssetAsync<AudioClip>(key);
    4.         AudioClip ac = op.WaitForCompletion();
    5.         Addressables.Release(op);
    6.         return ac;
    7.     }
    The code above works perfectly when running in Editor.

    However, when I run the same code built for Windows Build and step through with Unity Debugger, the object (ac) is still populated correctly by the WaitForCompletion() line, but then ac is reset to null by the Addressables.Release() line.

    If I comment out the Addressables.Release(op) line, then the code also works perfectly in Windows Build, so I know the setup of my Addressables and groups are correct. This makes no sense and is not a viable solution, since the Unity docs indicate that Addressables.Release is mandatory to prevent memory leaks. Any ideas?
     
  2. sauerkraut

    sauerkraut

    Joined:
    Feb 21, 2014
    Posts:
    23
    Still stuck on this. For now I am having to build with Release line commented out. Has noone else experienced this?
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,943
    Strictly speaking you only need to release the handle when the reference is no longer needed, though common practice is to get the ref, release the handle, return the reference.

    Editor behaviour is different to build behaviour, in particular with addressables. By default in editor it's just using asset database. That said, this should be working.

    So obvious first question, are you building your addressables before building your game?
     
    KAJed likes this.
  4. pillakirsten

    pillakirsten

    Unity Technologies

    Joined:
    May 22, 2019
    Posts:
    346
    Hi @sauerkraut I suspect that you are using the "Use Asset Database" playmode script in the Editor, which loads assets from the Asset Database. If you switch to the "Use Existing Build" playmode script, the assets will load from your built AssetBundles just like in your game build. More info about playmode scripts here https://docs.unity3d.com/Packages/com.unity.addressables@1.20/manual/Groups.html#play-mode-scripts

    As mentioned in the comment above, you would want to use Release when the asset loaded is no longer needed and can be unloaded from memory. In this example Release is called when the asset is destroyed:
    https://docs.unity3d.com/Packages/c...AddressableAssets.html#loading-a-single-asset