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

               GtkWidget *mybutton;

               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.

Object hierarchy

GObject

╰── GInitiallyUnowned

╰── GtkWidget

├── GtkContainer

├── GtkMisc

├── GtkCalendar

├── GtkCellView

├── GtkDrawingArea

├── GtkEntry

…………..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.

GtkAction

GtkListStore

GtkPrinter

GtkStyle

GtkTextBuffer

GtkTextMark

GtkTextTag

GtkTreeSelection

GtkTreeStore

GtkUIManage

Object memory management

gpointer    g_object_ref(gpointer  object);   

This will increment the reference count of objcet

gpointer    g_object_ref_sink(gpointer  object);

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

#include<gtk/gtk.h>
#include<gio/gio>
gboolean ref; 
int main( int argc, char *argv[]) 
{ 
  GtkWidget *window; 
  GtkWidget *fixed; 

  GtkWidget *button1; 
  GtkWidget *button2; 

  gtk_init(&argc, &argv); 
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
        ref=g_object_is_floating(window); 
        if(ref) 
          g_print(" window  is floating \n"); 
        else 
          g_print("window  is not floating \n"); 

  gtk_window_set_title(GTK_WINDOW(window), "GtkFixed"); 
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); 

  fixed = gtk_fixed_new(); 
        ref=g_object_is_floating(fixed); 
if(ref) 
          g_print("fixed is floating \n"); 
        else 
          g_print("fixed is not floating \n"); 
  gtk_container_add(GTK_CONTAINER(window), fixed); 

  button1 = gtk_button_new_with_label("Button"); 
        ref=g_object_is_floating(button1); 
if(ref) 
          g_print("button is floating \n"); 
        else 
          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); 
  g_signal_connect_swapped(G_OBJECT(window), "destroy", 
      G_CALLBACK(gtk_main_quit), NULL); 
  gtk_widget_show_all(window); 
  gtk_main(); 
  return 0; 
} 

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

References
Glib

http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html
http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html#g-strdup-printf

GObject

http://library.gnome.org/devel/gobject/stable/gobject-The-Base-Object-Type.html
http://library.gnome.org/devel/gobject/stable/gobject-The-Base-Object-Type.html#g-object-get

Gtk

http://library.gnome.org/devel/gtk/stable/ch01.html
http://library.gnome.org/devel/gtk/stable/GtkTreeModel.html#gtk-tree-model-get



Related Contents to follow