RE: How to orderly empty a tree view without memory leakage?



What am I supposed to do in order to cleanly free the 
contents of a tree
view without memory leakage in GTK+ 2.2.? I have a tree view 
which is to
be refilled with completely different data when the user of my program
selects different data sources.

The first thing to remember is GTK takes a copy of all the data you pass it,
hence, you are expected to free all memory you create, NOT all memory GTK
creates.


My approach was to
1. gtk_tree_view_remove_column (tv, column) for every
   gtk_tree_view_get_column (tv, 0) there is,
3. gtk_tree_view_set_model (tv, NULL) to free(?) the existing model.

The easiest way to do this is to use the gtk_tree_store_clear() or
gtk_list_store_clear() function.  This removes the data.  See here:
http://developer.gnome.org/doc/API/2.0/gtk/GtkListStore.html#gtk-list-store-
clear

Adding and removing columns is only necessary if you change the columns
based on the data being displayed.  If the data is similar, it is probably
best to hide/show columns as necessary.  If the data is completely
different, I would suggest having two different treeviews and hiding/showing
which ever one is required at the time.


However, there is still a noticeable increase of memory 
consumption by the
program, each time when I refill the tree view. I then added step 2:

2. gtk_tree_view_get_model (tv) and g_object_unref () it!

When you use gtk_tree_store_new() or gtk_list_store_new(), there are two
references on the store.  One is GTKs reference and the other is for you
(although a quick look at the gtk_list_store_new() API documentation does
not state this and I think it should).

As the demo (gtk-demo) demonstrates, you should free your copy if you wish
to no longer keep it around. - This is usually after creation.

  g_object_unref'ing the model reproducible for some of you 
or am I wrong?
- Am I really supposed to manually g_object_unref () the model?
- Should I manually g_object_unref () more objects, for instance the
  columns, before removing them or is it a bug in GTK+ 2.2 that models
  have to be unref'd manually to prevent memory leaks?

See the documentation.  It *SHOULD* state those objects which should be
unreferences, all others should not.  If there is something that is not
documented that should be unreferenced, it is a bug in GTKs documentation
and should be reported to http://bugzilla.gnome.org

- What about cell renderers? Are they (supposed to be) unref'd / freed
  implicitly when all columns are removed or should I (try 
to) unref them?

No, not as far as I am aware.

Regards,
Martyn



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]