This is something I noticed playing around in valgrind and seems to be quite common in GTK+ code. When you retrieve a string from a GtkTreeView, YOU are responsible for freeing it:


gchar* my_string
gtk_tree_model_get (model, COLUMN_MY_STRING, &my_string, -1);
g_message ("Printing my string: %s", my_string);
/* Do something more useful
------------------------------- */
g_free (my_string); // Important, otherwise YOU leak memory.

In the hope it is correct and helps some people…

5 Responses to “Avoiding memory leaks with GtkTreeView”

  1. bjoern Says:

    Hi Johannes,

    it is possible to scan gnome-svn for such code fragment?
    Maybe, we can start a gnome-goal to fix that.


  2. hmm, might be difficult to do this in an automated way as there might be a lot of code depending on the string before I can be free’d. But maybe some grep wizards might even be able to do this. Anyway, I think it’s better to check your code with valgrind to fix those (and other bugs). This also gives you hints on invalid reads/writes.

  3. Ross Says:

    Strictly speaking, you free all memory you get from gtk_tree_model_get(). So you free strings, unref objects, unref boxed types, and so on. Obviously literal pointers and integer types don’t need any special work.


  4. Or just use gtkmm. :-P

  5. Thomas Says:

    Shouldn’t a valgrind plugin of some sort be able to detect that ?


Comments are closed.