[Gtk-sharp-list] Object destruction

Victor Rafael Rivarola Soerensen (FANATICO y LOCO por Cristo) vrrivaro at gmail.com
Sun Jul 16 14:38:59 EDT 2006

2006/7/16, Thorsten Schoel <thorsten.schoel at lycos.de>:
> Hello,
> I stumbled upon a behaviour of Gtk# that seems to be quite problematic
> to me:
> When a Widget is removed from a container and no more obvious references
> to it exist it is still not destroyed. No Destroy or DestroyEvent event
> is emitted. Also if you encapsulate a widget in a new class its
> destructor isn't called, even after a call to System.GC.Sollect(). This
> is in contrast to the same code written in C. I have attached three
> examples: test.c showing the (correctly working) C-code, test.cs showing
> the (not correctly working) C#-code using a simple Gtk#-object and
> test2.cs showing the (not working correctly as well) C#-code using an
> encapsulated object - in this last case the destructor is only called
> when the whole program exits as its output is
> Quit
> Exit
> Destructor called!
> What the program does is simply remove a button from the window when it's clicked.
> My question is if this behaviour is a bug in Gtk# (it would be a rather
> serious one in my opinion) or if I made some mistake in my code. Since I am somehow confused about this behaviour of Gtk# I would appreciate any comments that could enlighen me here.
> Thank you

This is not a bug, but a C· fetaure ;-) by design.

Destructors are not called until the memoty is needed. Period.

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
program is call the methods:

button.Dispose ();
wnd.Dispose ();

Jesus bless you,

Victor Rivarola

"Por cuanto eres tibio, y no frío ni caliente, te vomitaré de mi boca."
Apocalipsis 3:16

"Porque la Palabra de la Cruz es locura para los que se pierden; pero a
los que se salvan, esto es, a nosotros, es poder de Dios."
1 Corintios 1:18

More information about the Gtk-sharp-list mailing list