Search Unity

releasing render texture that is set as Camera.targetTexture!

Discussion in 'Scripting' started by BisuDagger, May 9, 2016.

  1. BisuDagger

    BisuDagger

    Joined:
    May 2, 2014
    Posts:
    31
    Hello all,
    releasing render texture that is set as Camera.targetTexture is a problem that has popped up for many people with no solution. When you create a render texture in code, for example:
    Code (csharp):
    1.  
    2.   // Create a render texture
    3.   panelTexture = new RenderTexture((int)imageSize.x, (int)imageSize.y, 24, RenderTextureFormat.ARGB32);
    4.   panelTexture.antiAliasing = 2;
    5.   panelTexture.Create();
    6.   panelImage.GetComponent<RawImage>().texture = panelTexture;
    7.  
    8.   panelInfo.panelCamera.targetTexture = panelTexture;
    9.  
    If you press "Alt +Enter" to toggle fullscreen or not, then the "releasing render texture that is set as Camera.targetTexture" error will pop up (I'm using SRDebugger to determine this). The result is the render texture turning completely white and all objects involved with that object like my panelCamera.targetTexture no longer have a reference to it.

    Can anyone out there suggest a way to keep the render texture during full screen toggle?
     
  2. BisuDagger

    BisuDagger

    Joined:
    May 2, 2014
    Posts:
    31
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    I'm not aware of this issue. Have you filed a bug report?
     
  4. BisuDagger

    BisuDagger

    Joined:
    May 2, 2014
    Posts:
    31
  5. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Yes, this has been a bug for a very long time.
     
  6. BisuDagger

    BisuDagger

    Joined:
    May 2, 2014
    Posts:
    31
    That is correct. They fixed it on I believe 5.4. Thanks for the reply.
     
  7. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I experienced it in 5.5 yesterday
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    Did anyone file a bug report?
     
  9. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I can't reproduce it. It's random. It happened twice, when I Saved the scene. But it doesn't happen all the time. There doesn't seem to be any rhyme or reason.
     
  10. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    I'm getting this error when saving my scene. All of the Cameras lose their references to the RenderTexture upon saving. The project's quite big so I don't think I'll be able to file a report, seems random.
     
    infinitypbr likes this.
  11. Deleted User

    Deleted User

    Guest

    Getting this in 5.6.0f3
    I have a script(ExecuteInEditMode) that creates RenderTexture's and assigns them to cameras on the same GameObject.
    They seem to get released(inconsistently) when I play/stop.
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    Can you file a bug report please?
     
  13. Deleted User

    Deleted User

    Guest

    Will try to repro/submit later if I have time.
     
    Jodon and karl_jones like this.
  14. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    Hi
    Just sent an bug report with this error.
    Almost empty scene while testing HD Pipeline with 2018.2.0f2
    Target Texture field under Camera Output Settings is empty of course
     
    Last edited: Jul 13, 2018
    OfficialHermie likes this.
  15. mephistonight

    mephistonight

    Joined:
    Dec 14, 2016
    Posts:
    75
    Would you mind linking your issue number so I can also track it please?
     
  16. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    I've filed a bug report.
     
  17. TokyoWarfareProject

    TokyoWarfareProject

    Joined:
    Jun 20, 2018
    Posts:
    814
    2018.3 when coming back from gameplay to main menu I get this err. In gameplay there is a minimap that uses a render texture.
     
  18. raysainthyl

    raysainthyl

    Joined:
    May 8, 2019
    Posts:
    7
    I encounter the same problem using 2018.3. do you find the solution?
     
  19. TokyoWarfareProject

    TokyoWarfareProject

    Joined:
    Jun 20, 2018
    Posts:
    814
    no, still there in 2018.4, seems harmless tho, still an sception in console...
     
  20. raysainthyl

    raysainthyl

    Joined:
    May 8, 2019
    Posts:
    7
    thx for reply. On some android phones, i guess this problem leads to crash of my game
     
  21. TokyoWarfareProject

    TokyoWarfareProject

    Joined:
    Jun 20, 2018
    Posts:
    814
    I see, I target PC and Xbox, no crashes there.
     
  22. ruudvangaal

    ruudvangaal

    Joined:
    May 15, 2017
    Posts:
    27
    I noticed we get this error when the following is used:
    - you have made a RenderTexture in your project (an asset)
    - you have a Camera in your scene which uses this render texture
    - a material is added which refers to this render texture.

    When calling Destroy() on such a GameObject, the behavior in 2018.3.9f1 is this:
    - From the root down, OnDisable() gets called.
    - Meanwhile, resources like render textures are being released by Unity.
    - Once you get to a camera with a targetTexture (renderTexture), Unity gives an error about Camera.targetTexture not being null, but already removed (renderTexture).
    - After that, the OnDisable() is called for the camera itself, so you're too late to set targetTexture to null.


    The fix for us was to add a small script to the top GameObject which sets targetTexture to null for all Camera's in its children involved:

    Code (CSharp):
    1. using UnityEngine;
    2. public class CameraCleaner : MonoBehaviour
    3. {
    4.   void OnDisable()
    5.   {
    6.     Debug.LogWarning($"GameObject '{name}' OnDisable is called (CameraCleaner)");
    7.     var comp = FindObjectOfType<Camera>();
    8.     if (comp != null)
    9.     {
    10.       Debug.LogWarning("Setting all Camera.targetTexture fields to null");
    11.       var cams=this.GetComponentsInChildren<Camera>();
    12.       foreach(var cam in cams)
    13.       {
    14.         Debug.LogWarning($"Found camera '{cam.name}' - resetting targetTexture");
    15.         cam.targetTexture = null;
    16.       }
    17.     }
    18.   }
    19. }
     
    Last edited: Oct 12, 2021
  23. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    2020.3 and getting this bug when returning to our main menu.
    Did anyone get a response on their bug reports?
     
  24. TokyoWarfareProject

    TokyoWarfareProject

    Joined:
    Jun 20, 2018
    Posts:
    814
    Funny, I also get this when returning to main menu, well tbh, it has disapeared latelly but it triggered when going back to menu from gameplay. I've one RT for the minimap, not sure if its involved
     
  25. VitaliyTheCat

    VitaliyTheCat

    Joined:
    Jul 4, 2021
    Posts:
    2
    I found a way to get rid of it in my case! Change the render texture's "Initialization Mode" from "Realtime" to "OnDemand". ;)

    It seems to work with "OnLoad" as well, although I haven't tested this as thoroughly.
     
  26. TokyoWarfareProject

    TokyoWarfareProject

    Joined:
    Jun 20, 2018
    Posts:
    814
    I can´t see such option
    upload_2021-7-5_12-43-39.png
     
  27. VitaliyTheCat

    VitaliyTheCat

    Joined:
    Jul 4, 2021
    Posts:
    2
    Ahhhh, I guess the standard Render Texture might not have that option. Try creating a "Custom Render Texture" instead, and set it up with the same settings. Here's what mine looks like:
    CustomRenderTexture.png

    I hope this helps!
     
  28. mediamax07

    mediamax07

    Joined:
    May 22, 2017
    Posts:
    7
    Make sure you set RenderTexture.active to null and the warning will disappear
     
  29. samuel-levine

    samuel-levine

    Joined:
    Sep 1, 2012
    Posts:
    11
    Hello! This is an old thread, but wanted to throw in my fix as well.

    ruudvangaal led the way, but at least in Unity 2021.3.14f1, OnDisable comes in too late, so the error persisted with that snippet.

    This one worked for us, when attached to a gameobject ancestor of the offending Camera using the RT (in our case, a Mirror):

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RenderTextureCleaner : MonoBehaviour
    4. {
    5.     void OnApplicationQuit() {
    6.         var cams = GetComponentsInChildren<Camera>();
    7.         foreach (var cam in cams) {
    8.             Debug.Log("Found camera " + cam.name + ", resetting targetTexture!");
    9.             cam.targetTexture = null;
    10.         }
    11.     }
    12. }
     
    JanetGilbertPraxis likes this.
  30. ruudvangaal

    ruudvangaal

    Joined:
    May 15, 2017
    Posts:
    27
    We noticed that too in Unity 2021.3.8f1, that the error came back compared to Unity 2019.4. We tend to keep Unity running for a long time, so OnApplicationQuit() will come a bit late for us I'm afraid. The current fix is to use the same CameraCleaner class but in that class to also add 'cam.targetTexture.Release()' to avoid the rendertexture leaking. But then to also call this (between game sessions in our case) to find the prefab templates and fix those as well (not just the GameObject which is named 'myname(Clone)' when instantiated from prefab 'myname'):

    Code (CSharp):
    1. static string GetFullPath(Transform tf)
    2. {
    3.   string s = "";
    4.   if (tf.parent != null)
    5.     s = GetFullPath(tf.parent) + "/" + tf.gameObject.name;
    6.   else
    7.     return tf.gameObject.name;
    8.   return s;
    9. }
    10.  
    11. public static void CleanCamerasGlobally()
    12. {
    13.   string goName;
    14.  
    15.   Debug.Log($"CameraCleaner: finding Camera's all over the place");
    16.   var cams = Resources.FindObjectsOfTypeAll<Camera>();
    17.  
    18.   foreach (var cam in cams)
    19.   {
    20.     goName = GetFullPath(cam.gameObject.transform);
    21.  
    22.     if (cam.targetTexture == null)
    23.     {
    24.       Debug.Log($"- Found camera '{cam.name}' in GameObject '{goName}' but targetTexture is already null");
    25.     }
    26.     else
    27.     {
    28.       Debug.Log($"- Found camera '{cam.name}' in GameObject '{goName}'; releasing and resetting targetTexture");
    29.       cam.targetTexture.Release();
    30.       cam.targetTexture = null;
    31.     }
    32.   }
    33. }
    I'm a bit afraid though that the Resources.FindObjectsOfTypeAll() call also finds on-disk prefabs and such and modifies those. I'm not entirely sure how this internally works; 'Resources.xxx' works on scene objects as well as assets so you can get a bit more returned than you expect. On the other hand, just using GameObject.Find() will not give you the prefab, which in our case seemed to generate the 'releasing render texture that is set as Camera.targetTexture!' error.

    On 2nd thought, you may always want to set 'cam.targetTexture' to null, since it is a RenderTexture, which is derived from Object, which fakes null comparisons. So you might leak the cam.targetTexture object on the C# side if not setting cam.targetTexture to null.
     
    samuel-levine likes this.
  31. levoxtrip

    levoxtrip

    Joined:
    May 4, 2020
    Posts:
    48
    This sadly is not solving my problem with it. But much appreciated
     
    ruudvangaal likes this.
  32. ichiyaman_

    ichiyaman_

    Joined:
    May 30, 2019
    Posts:
    2
    Hi, It's works form me. Thanks a lot.
     
    ruudvangaal likes this.