[gtkada] Garbage collection issues?

briot at gnat.com briot at gnat.com
Tue Feb 29 10:01:14 CET 2000


 > Here is a cluster of observations and questions about memory management

I think this should go into a FAQ, this is a question that many people have
asked in the past. Unfortunately, we do not have a FAQ yet :-)

 > Some of the example programs shipping with GtkAda use a single variable
 > to catch the handles of multiple widgets as they are created, with the
 > result that the program afterward only has direct access to the last one
 > in the sequence.  Is that strategy suitable for a large program that may
 > run for hours and delete various windows along the way?  I.e., does
 > GtkAda (or the underlying GTK+) do any kind of automatic garbage
 > collection?

The short answer is "yes".
GtkAda takes care of almost all the memory management for you.
Here is a brief overview of how this works, you'll have to check the sources
if you want more detailled information.
gtk+ (the C library) does its own memory management through reference
counting, ie any widget is destroyed when it is no longer referenced anywhere
in the application. So as far as C objects are concerned, we did not have any
thing to do.
Now in GtkAda we associated a "user_data" which each C object that points to
the Ada tagged type that was allocated in the Gtk_New procedure. We also
register a "destroy" callback to be called when the object to which the
user_data belongs is destroyed (see Gtk.Initialize_User_Data).
Thus, every time a C object is destroyed, the equivalent Ada structure is also
destroyed (see Gtk.Free_User_Data).


 > If I delete a top-level window (or container), are the resources for its
 > component parts also deleted? Or do I need to create procedures to
 > recursively destroy all the lower-level components?

Every container holds a reference to its children, whose reference counting is
thus different from 0 (and generally 1). When the container is destroyed, the
reference of all its children and grand-children is decremented, and they are
destroyed in turn if needed.
So no need to create a complex procedure...

 > Finally, are all resources freed up when a program exits?  What if the
 > program exits abnormally?

I believe they should, but someone more knowledgeable about the inner workings
of X servers should answer (are the ressources automatically freed when the
application to which they belong crashes ?)

Emmanuel





More information about the gtkada mailing list