Search Unity

Release asset is not aware of object which has been loaded before?

Discussion in 'Addressables' started by pahe, May 8, 2020.

  1. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Hi guys.

    I'm currently writing a couple of unit tests and one of the is failing for me and I don't understand exactly what I am doing wrong here. My code looks like:

    Code (CSharp):
    1.         public IEnumerator TestUnloadingAssetAsync()
    2.         {
    3.             string assetName = "Assets/Scenes/TestScene/TestTexture.png";
    4.             Texture loadedTexture = null;
    5.             int referenceCount = 0;
    6.             AsyncOperationHandle<Texture> handle = addressablesLoader.LoadAssetAsync<Texture>(assetName, texture => loadedTexture = texture);
    7.          
    8.             PropertyInfo propInfo = handle.GetType().GetProperty("ReferenceCount", BindingFlags.NonPublic | BindingFlags.Instance);
    9.  
    10.             while (loadedTexture == null)
    11.             {
    12.                 yield return null;
    13.                 referenceCount = (int)propInfo.GetValue(handle);
    14.             }
    15.  
    16.             Assert.IsTrue(referenceCount > 0); // is 1 and that's fine!
    17.  
    18.             Addressables.Release(handle); // doesn't work neither???
    19.             referenceCount = (int)propInfo.GetValue(handle);
    20.  
    21.             Assert.IsTrue(referenceCount == 0);
    22.         }
    With the error message:

    Addressables.Release was called on an object that Addressables was not previously aware of. Thus nothing is being released
    UnityEngine.AddressableAssets.Addressables:Release(AsyncOperationHandle`1)


    What am I doing wrong? In my understanding, the reference count should be 0 after releasing the asset again.

    Thank you for help,
    Patrick
     
    Last edited: May 15, 2020
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,820
    I'll flag this for the team to have a look! Which version of Addressables are you using?
     
  3. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Unity 2019.3.0f3 and Addressables 1.8.3
     
  4. CameronND

    CameronND

    Joined:
    Oct 2, 2018
    Posts:
    88
    What is addressablesLoader?
    Does it work if you call handle.Release instead?
     
  5. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Sorry, it's just a wrapper class around the Addressables API. I updated the code above. It's just calling
    Addressables.Release(obj);
    .

    And I can't call
    handle.Release
    as handle is a
    AsyncOperationHandle<Texture>
    which doesn't have a Release method or did I get you wrong?
     
  6. SviperSniper

    SviperSniper

    Joined:
    Mar 1, 2017
    Posts:
    37
    Hey,
    You can use the following to release the handle:
    Code (CSharp):
    1. Addressables.Release(handle)
    .
     
  7. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    That's exactly what is not working :)
     
  8. SviperSniper

    SviperSniper

    Joined:
    Mar 1, 2017
    Posts:
    37
    ...Sorry, my bad... xD

    There was a bug in the Release method in 1.6.2 where generic handles were not converted correctly to non-generic handles.

    You could try to use AsyncOperationHandle instead of AsyncOperationHandle<Texture> just to check if it is something similar.
     
  9. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    No worries. And sadly I tried that also and it didn't work neither.
     
  10. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Hey @pahe you bring up an interesting point. So, after releasing a handle to the point that its internal operation would be 0, we actually reset the reference count to 1 after we've unloaded the asset bundles, run any Destroy code, and invalidated handles (when applicable).

    The error message you're getting makes me curious, however. Given that you're seeing that it may not be the case that the reference count is 1 because of the reason above or if its truly not getting released properly. I feel like we need to look more into it. Would you file a bug and attach your project if at all possible?