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

ScriptableObject Out Of Scope / Memory Help

Discussion in 'Scripting' started by karljj1, Mar 2, 2013.

  1. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    Hi,

    I have several classes that inherit from ScriptableObject. Some of these classes have properties that are also derived from ScriptableObject.

    When the OnDestroy function is called I can Destroy the properties however what if one of my properties is being referenced by multiple objects? How can I know if its safe to destroy the object?
    The doumentation says the OnDisable function is called when a ScriptableObject goes out of scope however i have not seen this happen so far.
    How would i know if a property was being referenced elsewhere? I could implement a reference counting system but would prefer not to.

    Here is an example:

    Code (csharp):
    1.  
    2. public class ClassA : ScriptableObject
    3. {
    4.     ~ClassA()
    5.     {
    6.         Debug.Log( "Deleted Class A" );
    7.     }
    8.  
    9.  
    10.     void OnDisable()
    11.     {
    12.         Debug.Log( "OnDisable Class A" );
    13.     }
    14.    
    15.     void OnDestroy()
    16.     {
    17.         Debug.Log( "OnDestroy Class A" );
    18.     }
    19. }
    20.  
    Code (csharp):
    1.  
    2. public class ClassB : ScriptableObject
    3. {
    4.     public ClassA classA;
    5.    
    6.     ~ClassB()
    7.     {        
    8.         Debug.Log( "Deleted Class B" );
    9.     }
    10.  
    11.  
    12.     void OnDisable()
    13.     {
    14.         Debug.Log( "OnDisable Class B" );
    15.     }
    16.    
    17.     void OnDestroy()
    18.     {        
    19.         Debug.Log( "OnDestroy Class B" );
    20.     }
    21. }
    22.  
    My Test class
    Code (csharp):
    1.  
    2.  
    3.  
    4. public class MemoryTest : MonoBehaviour
    5. {
    6.     public ClassB bRef1;
    7.     public ClassB bRef2;
    8.  
    9.  
    10.     IEnumerator Start()
    11.     {
    12.         bRef1 = ScriptableObject.CreateInstance<ClassB>();
    13.         bRef2 = ScriptableObject.CreateInstance<ClassB>();
    14.  
    15.  
    16.         bRef1.classA = ScriptableObject.CreateInstance<ClassA>();
    17.         bRef2.classA = bRef1.classA; // Share class A;
    18.        
    19.         yield return new WaitForSeconds( 5 );
    20.  
    21.  
    22.         DestroyImmediate( bRef1 );
    23.         DestroyImmediate( bRef2 ); // Now classA should be destroyed but it is not. How can i know its safe to destroy??
    24.     }
    25. }
    26.  
    27.  
    28.  
    Thanks

    Karl
     
    Last edited: Mar 2, 2013
  2. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    So I was thinking of using a reference counting system, has anyone else tried this? Any implementation methods you could recommend?

    Karl
     
  3. warreneng

    warreneng

    Joined:
    Mar 13, 2017
    Posts:
    8
    I'm encountering a similar problem trying to hook into ScriptableObject's OnDisable message. I cannot discern when OnDisable gets called, even though the documentation says "when [it] goes out of scope." I understand that to mean when nothing's referencing the ScriptableObject.

    As a test I created 2 scenes. The first scene contains a MonoBehaviour with a serialized reference to a ScriptableObject asset in my project tree. The second scene is empty. When I play the first scene in the Editor and trigger a scene load into the 2nd scene, I do not get an OnDisable call to the ScriptableObject.

    So I am going to try manually implementing a reference counting system to do this instead.