x


IDisposable in Unityscript?

Does anyone know the syntax for an IDisposable class in UnityScript? The C# ~ callback doesn't work.

Thanks,

-Aubrey

more ▼

asked Dec 10 '10 at 06:27 AM

Aubrey Falconer gravatar image

Aubrey Falconer
657 45 53 68

What has multithreading with the question to do?

Dec 10 '10 at 02:11 PM Statement ♦♦

Umm ~ not much. Tag removed...

Dec 12 '10 at 05:07 PM Aubrey Falconer
(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest
  • If you mean the finalizer (destructor) by "C# ~ callback", it is called at an seemingly arbitrary time after references to the object have been lost. So it gets called but maybe you expect them to behave differently.

  • There is a IDisposable interface in .NET. (System.IDisposable) that you can use in case you need to release unmanaged resources.

  • If you just want to destroy a script or a game object, use Destroy(scriptInstance); or Destroy(gameObject);

Points to remember:

  • Destructors are invoked automatically, and cannot be invoked explicitly.
  • Destructors cannot be overloaded. Thus, a class can have, at most, one destructor.
  • Destructors are not inherited. Thus, a class has no destructors other than the one, which may be declared in it.
  • Destructors cannot be used with structs. They are only used with classes.
  • An instance becomes eligible for destruction when it is no longer possible for any code to use the instance.
  • Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
  • When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.
more ▼

answered Dec 10 '10 at 02:10 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thanks for all the info, but I am working in Unity Javascript. The ~ destructor doesn't compile, and I am interested in the syntax for the destructor in UnityScript. The following code compiles fine, but it never prints anything even long after the reference to the object has been destroyed.

class WhirldIn extends System.Object implements System.IDisposable { function Dispose() { Debug.Log("Farewell, Cruel World!"); } }

Dec 11 '10 at 04:59 PM Aubrey Falconer

IDisposable implys that you manually call Dispose(). I wouldn't trust any destructors in unity. If you need to make cleanup when an object is destroyed you have to manually call such method. I made an extension method (In C#) that is called Dispose() which calls BroadcastMessage ("OnDispose".. and after that Destroy that object.

Dec 12 '10 at 05:14 PM Statement ♦♦

Do you mean it doesn't get called when you do objectReference = null or when you do Destroy(objectReference)?

Dec 12 '10 at 05:15 PM Statement ♦♦

A destructor would work fine for the class he created above (which doesn't inherit MonoBehaviour - that'd be awful otherwise).

Dec 12 '10 at 05:28 PM Mike 3
(comments are locked)
10|3000 characters needed characters left

I'm not sure what callback you're talking about, but an IDisposable class is just one that implements the IDisposable interface and has a public Dispose method:

public class MyClass : IDisposable
{
    public void Dispose()
    {
    }
}

Edit:

IDisposable isn't what you're looking for at all, you're wanting the finalizer syntax. Try this instead:

function Finalize()
{
}
more ▼

answered Dec 10 '10 at 02:01 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x108

asked: Dec 10 '10 at 06:27 AM

Seen: 1952 times

Last Updated: Dec 12 '10 at 05:06 PM