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

I am using the recommended code structure (shown below) for loading Addressables with WaitForCompletion.

void Start()
{
    //Basic use case of forcing a synchronous load of a GameObject
    var op = Addressables.LoadAssetAsync<GameObject>("myGameObjectKey");
    GameObject go = op.WaitForCompletion();

    //Do work...

    Addressables.Release(op);
}

My specific implementation is below:

    public AudioClip LoadAudioClip(string key)
    {
        var op = Addressables.LoadAssetAsync<AudioClip>(key);
        AudioClip ac = op.WaitForCompletion();
        Addressables.Release(op);
        return ac;
    }

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?

Still stuck on this. For now I am having to build with Release line commented out. Has noone else experienced this?

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?

1 Like

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 Groups | Addressables | 1.20.5

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/com.unity.addressables@1.20/manual/LoadingAddressableAssets.html#loading-a-single-asset