[Gtk-sharp-list] Object destruction

Michael Hutchinson m.j.hutchinson at gmail.com
Sun Jul 16 15:08:15 EDT 2006

On 7/16/06, Victor Rafael Rivarola Soerensen (FANATICO y LOCO por
Cristo) <vrrivaro at gmail.com> wrote:
> This is not a bug, but a C· fetaure ;-) by design.
> Destructors are not called until the memoty is needed. Period.

Yes, it's called 'nondeterministic finalisation'. The finaliser will
be called, but there is no guarantee *when* it will be called.

> Except the objects which implement the IIsDisposable interface.
> Suppose the Gtk.Button class implements this interface (I don't
> remember if it does or doesn't). All you would have to do in your

Yes, the base class Glib.Object does. There's also the explicit
Gtk.Destroy method (), though that does not work as you might expect.

> program is call the methods:
> button.Dispose ();
> wnd.Dispose ();

I'm not certain about this, but I think that the IDisposable pattern
requires a disposed object to dispose its children, so you may not
need to call button.Dispose ().

Related info: The Dispose method is expected to be called by the
object's finaliser if it is not explicitly called. If it is explicitly
called, then it is expected to suppress the object's finaliser.
C++/CLI implements deterministic finalisation by implementing the
IDisposable pattern behind the scenes. C# also has built in
IDisposable support with the 'using' keyword.

Michael Hutchinson

More information about the Gtk-sharp-list mailing list