Search Unity

C# destructor question

Discussion in 'Scripting' started by Alexo, Sep 25, 2008.

  1. Alexo

    Alexo

    Joined:
    Feb 26, 2007
    Posts:
    34
    Hi to everyone !!!

    I'm trying to make a DEBUG MODE for my project in which I created a static C# class that provides an ArrayList (to store the informations I need to display in a Debug window) and some others accessories functions. Pratically I'm trying to make my own "Stats" window.

    Here's an example of how to use the static class:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnyClassOfProject : MonoBehaviour, ... {
    5.    .
    6.    .
    7.    .
    8.    // all the debug value to show if DEBUG MODE is enabled
    9.    private DebugData dd1;
    10.    private DebugData dd2;
    11.    private DebugData dd3;
    12.    .
    13.    .
    14.    .
    15.    ~AnyClassOfProject () {
    16.       if (DebugManager.DEBUG_ENABLED)
    17.          DebugManager.removeDebugData (dd1);
    18.          DebugManager.removeDebugData (dd2);
    19.          DebugManager.removeDebugData (dd3);
    20.          .
    21.          .
    22.          .
    23.       } // if
    24.    } // destructor
    25.  
    26.    void Start () {
    27.       .
    28.       .
    29.       .
    30.       if (DebugManager.DEBUG_ENABLED) {
    31.          dd1 = DebugManager.createDebugData ("string1 to show in DebugWindow");
    32.          dd2 = DebugManager.createDebugData ("string2 to show in DebugWindow");
    33.          dd3 = DebugManager.createDebugData ("string3 to show in DebugWindow");
    34.          .
    35.          .
    36.          .
    37.       } // if
    38.    } // Start
    39.    
    40.    void Update () {
    41.       .
    42.       .
    43.       .
    44.       if (DebugManager.DEBUG_ENABLED) {
    45.          dd1.setDataValue ("the value of data1 to show");
    46.          dd2.setDataValue ("the value of data2 to show");
    47.          dd3.setDataValue ("the value of data3 to show");
    48.          .
    49.          .
    50.          .
    51.       } // if
    52.    } // Update
    53.  
    54.    .
    55.    .
    56.    .
    57.    
    58. } // AnyClassOfProject
    The problem is that the destructor of this class doesn't seem to call the static method of the static class to remove the information from the ArrayList. To tell you the thruth I've also tried to check the if (in whitch I try to check a static boolean value of the static class) and i found that the destructor executes the external instruction but not the ones in that if (it seems it cannot check the static variable of the static class).
    Just to be precise, I also tried to make all the deletions out the if but it doesn't work.

    Please, can someone help me to understand what's wrong?
    If it can help, I can also post the static class... just ask me.

    THANKS A LOT GUYS !!! :wink:
     
  2. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I presume you might need to not have this static but instead have it as a regular class object and instansiate it. As far as I am aware (and please someone knock me out if I am wrong), once the program starts, the static class is never destroyed until the program is terminated.

    Have a static array that stores elements generated by the debug class. So say you instansiate the class in a component, then fill a static array with elements generated by that class object. When the component gets destroyed, the destructor will trigger.

    HTH
    or let me know if I am way off in what you are trying to do...
     
  3. marraboy

    marraboy

    Joined:
    Mar 25, 2008
    Posts:
    113
    Here's a shot in the dark:

    Is the destructor actually being called? Have you put Debug.Log("I am being destroyed dude!"); in the destructor?

    The reason I ask is:

    An object is eligible for destruction when it is no longer possible for any code to use the instance... Well your static class can use the instance (the DebugData objects).

    Are you sure it is being destroyed?!?

    I think you are right about the static class but it's not the static class he's having problems with. ;)

    Ta

    JT
     
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    You shouldn't use a constructor or destructor with any class derived from MonoBehaviour as those functions will be called at unexpected times. In the editor, this can even happen while the game isn't playing.

    The constructor is generally replaced with Start or Awake, and the destructor can be replaced with OnDisable. OnDisable fires when a script's .enabled member is set to false, but it also fires when the object is destroyed, so it's useful for this purpose.

    You can also use OnApplicationQuit if you just want to clean up at the end of the game.
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    I don't see why you'd want to do manual cleanup here though? You've got the garbage collector doing that for you.
     
  6. Alexo

    Alexo

    Joined:
    Feb 26, 2007
    Posts:
    34
    Because I've an ArrayList that store the pointer of all the DataDebug object of the project... so... if i don't remove this pointer from the ArrayList they will remain in it and they will be displayed in the Debug window even if they're no more usefull !!! :wink:

    However, I'll try some of the NCarter proposals solutions and I'll let you know !!!

    THANKS to everyone.
     
  7. Alexo

    Alexo

    Joined:
    Feb 26, 2007
    Posts:
    34
    Ok guys... to make that piece of code works you simply need to change this function

    Code (csharp):
    1. ~AnyClassOfProject () {
    2.    if (DebugManager.DEBUG_ENABLED) {
    3.       DebugManager.removeDebugData (dd1);
    4.       DebugManager.removeDebugData (dd2);
    5.       DebugManager.removeDebugData (dd3);
    6.       .
    7.       .
    8.       .
    9.    } // if
    10. } // destructor
    with this one

    Code (csharp):
    1. public void OnDisable () {
    2.    if (DebugManager.DEBUG_ENABLED) {
    3.       DebugManager.removeDebugData (dd1);
    4.       DebugManager.removeDebugData (dd2);
    5.       DebugManager.removeDebugData (dd3);
    6.       .
    7.       .
    8.       .
    9.    } // if
    10. } // OnDisable
    :wink:

    I think that this will work only if the class exends MonoBehaviour... but what if it doesn't? I make all this to have the possibilities to check the varibles values that are not possible to check in the inspector Debug view: some classes of my project are not attached to a game object so I've to use Debug.Log(string) funcion. That's not so comfortable if you cannot move your eyes from the scene to check what will be the game objects behaviour; so I create a window that is able to show in realtime what I need to know (e.g. the FPS rate, some boolean flags value, etc.).