Search Unity

Bug Error when releasing RTHandle of RenderTexture asset

Discussion in 'Universal Render Pipeline' started by Hellfim, May 21, 2023.

  1. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    Hello! Suppose I have the following class
    Code (CSharp):
    1. public class MyMonoBehaviour: MonoBehaviour
    2.     {      
    3.         [SerializeField]
    4.         private RenderTexture _targetTexture;
    5.  
    6.         private RTHandle _targetRTHandle;
    7.  
    8.         private void OnEnable()
    9.         {
    10.             if (_targetTexture != null)
    11.             {
    12.                 _targetRTHandle = RTHandles.Alloc(_targetTexture);
    13.             }
    14.         }
    15.  
    16.         //Some unnecessary code
    17.  
    18.         private void OnDisable()
    19.         {
    20.             RTHandles.Release(_targetRTHandle);
    21.         }
    22. }

    They idea is that I have a _targetTexture set up as an asset. When script becomes active I get _targetTexture's RTHandle do some manipulation with it and as a good man I try to release it in OnDisable. However I'm not only getting RTHandle released, but also a following error: Destroying assets is not permitted to avoid data loss.
    I checked Release method implementation and it invokes CoreUtils.Destroy(m_RT) which attempts to destroy the asset (thankfully UnityEditor doesn't allow that and throws an error).

    I also see that the handle is still getting released, as that's happening one line before the destroy attempt.

    I believe that ther should be a separate Release method overload which allows to control user wether he wants to release an associated object and an RTHandle or just the RTHandle. Something like this:
    Code (CSharp):
    1.  
    2. public void Release(bool releaseObject = true)
    3. {
    4.    m_Owner.Remove(this);
    5.    if (releaseObject)
    6.    {
    7.       CoreUtils.Destroy(m_RT);
    8.    }
    9.    m_NameID = BuiltinRenderTextureType.None;
    10.    m_RT = null;
    11.    m_ExternalTexture = null;
    12. }
    [/solution]

    I'm using Unity 2023.2.0a15
     
  2. ThomasZeng

    ThomasZeng

    Unity Technologies

    Joined:
    Jun 24, 2019
    Posts:
    86
    Last edited: May 22, 2023
    Hellfim likes this.
  3. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    I didn't do Help -> Report bug routine