Managing gtk+ memory: application interview Question
GTK+ is cross platform graphical user interface library previously known as GIMP (GNU image manipulation program) toolkit. it is most popular GUI library along with Qt(Nokia) GUI library in open source community. Linux Gnome desktop (ubuntu,fedora,centOs and may more) is best example of its uses, which is written in gtk.
For beginner go to this site http://zetcode.com/tutorials/gtktutorial/ and learn basic gtk programming. Before learning memory management in gtk we should note following facts:
- All GTK objects inherit from the GObject class provided by library.
- GOJect is used to provide object-oriented C-based APIs
For example to create button in gtk
mybutton = gtk_button_new();
- All widgets in gtk is reference counted objects and reference counted means number of owners of widget.
- There are two types of reference counting in gtk library, floating and non floating.
- GObject is base class and all gtk widgets are direct or indirect descendant of GObject.
- The gtk widget which is direct descendant of GObject starts with reference count 1 and the widget which is indirect descendant of GObject starts with floating reference count 1.0 . Let us discuss how to handle reference count of gtk widget to avoid memory leak in gtk based program.
…………..see gtk reference for all objects
Most of gtk widgets are derived from GInitiallyUnowned object. some of gtk widgets are GObject that are not GInitiallyUnowned. for example below widgets are not derived from GInitiallyUnowned, they are direct descendants of Gobject.
Object memory management
<span style="color: #800000;">gpointer g_object_ref(gpointer object); </span>
This will increment the reference count of objcet
<span style="color: #800000;">gpointer g_object_ref_sink(gpointer object);</span>
[crayon-5a60ddeae4e35555336835 inline="true" class="western"]if
the object has a floating ref, it converts that reference to a normal ref
else it increases the ref count by one
void g_object_unref (gpointer object);
This will decrease the reference count of object, if ref count == 0, the object is destroyed
The GObject library also provides the function g_object_is_floating() which checks whether an object has a floating reference and returns true if it does.
Sample program to check floating or not floating reference of gtk widgets
int main( int argc, char *argv)
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_print(" window is floating \n");
g_print("window is not floating \n");
fixed = gtk_fixed_new();
g_print("fixed is floating \n");
g_print("fixed is not floating \n");
button1 = gtk_button_new_with_label("Button");
g_print("button is floating \n");
g_print("button is not floating \n");
gtk_fixed_put(GTK_FIXED(fixed), button1, 100, 50);
button2 = gtk_button_new_with_label("Button");
gtk_fixed_put(GTK_FIXED(fixed), button2, 15, 15);
compile above program
gcc simple.c -o simple
pkg-config --cflags --libs gtk+-3.0
output will be
window is not floating
fixed is floating
button is floating