GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun May 19, 2013 8:29 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Freeing resources stored in GtkListStore
PostPosted: Thu Dec 18, 2008 6:03 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
Hello,

Sorry, I read the manual and articles about GtkTreeView, but I still can't get it over my head - I use my ListStore to fill it with some images with text (a list of icons and descriptions) and populate the store like this:

Code:
   
void populate_store(void)
{
GtkWidget* pImage;
GtkTreeIter iter;

gtk_list_store_clear(pNoteListStore);
gtk_list_store_append (pNoteListStore, &iter);

   pImage = gtk_image_new_from_stock(GTK_STOCK_EDIT, GTK_ICON_SIZE_SMALL_TOOLBAR);

   gtk_list_store_set (pNoteListStore, &iter,
      COLUMN_CHECK, TRUE,
      COLUMN_NOTETYPE, gtk_image_get_pixbuf(pImage),
      COLUMN_SHORTTEXT, "some text",
      -1);
}


Such code is called many times, filling the list store with various data. Do I need to free the pImage in any way after I called gtk_list_store_set? Or it will be taken care of when the list will be cleared?

And is it the correct way to put an icon to list store? If not, which is?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 8:03 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
I think that you need to unref it with g_object_unref, see the last line here:
http://library.gnome.org/devel/gtk/stab ... -store-set

As for alternatives, if you're using stock icons, then you could map the "stock-id" property
http://library.gnome.org/devel/gtk/stab ... --stock-id
of the GtkCellRendererPixbuf to a column that holds a G_TYPE_STRING and set it that way:
Code:
liststore = gtk_list_store_new(GDK_TYPE_PIXBUF /* pixbuf to display */
                               G_TYPE_STRING /* stock id of pixbuf */);
/* ... more set up code ... */
gtk_tree_view_column_add_attribute(col, renderer, "stock-id", COLUMN_2);

http://library.gnome.org/devel/gtk/stab ... -attribute

EDIT: Here's an example that does this:
viewtopic.php?p=6148#6148


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 8:07 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Your code is leaking since pImage needs to be destroyed after it is not needed any more. But the catch is that you would need to destroy it after the pixbuf column in your store gets cleared, since gtk_image_get_pixbuf does not make a copy of a pixbuf but instead only returns pointer to it's internal buffer.

There are quite a few ways of displaying icon in treeview. The simplest solution for stock images is to connect GtkCellRendererPixbuf stock-id property to a G_TYPE_STRING model column, which holds id's (The size of the image can be controlled with stock-size property.) You may also be interested in icon-name and gicon properties. Docs: http://library.gnome.org/devel/gtk/stable/GtkCellRendererPixbuf.html

If the icon in need of displaying is not accessible using methods listed above, you may need to create a pixbuf from file and pack it into store. The simplest way of doing that is with gdk_pixbuf_new_from_file function. Then add pixbuf in your store and call g_object_unref on it once (this way you get rid of your own reference and the pixbuf will be automatically destroyed when removed from your model). More docs: http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-file-loading.html

EDIT: @dreblen: Damn ... ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 8:10 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
dreblen wrote:
As for alternatives, if you're using stock icons, then you could map the "stock-id" property
http://library.gnome.org/devel/gtk/stab ... --stock-id
of the GtkCellRendererPixbuf to a column that holds a G_TYPE_STRING and set it that way:
Code:
liststore = gtk_list_store_new(GDK_TYPE_PIXBUF /* pixbuf to display */
                               G_TYPE_STRING /* stock id of pixbuf */);
/* ... more set up code ... */
gtk_tree_view_column_add_attribute(col, renderer, "stock-id", COLUMN_2);

http://library.gnome.org/devel/gtk/stab ... -attribute

EDIT: Here's an example that does this:
viewtopic.php?p=6148#6148


Thanks for that! That's a really nicer way.

And as for unrefs - when I create my tree like this
Code:
model = create_notelist_model();
treeview = gtk_tree_view_new_with_model (model);


Do I need to unref or otherwise free the model variable?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 8:23 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Corvin wrote:
And as for unrefs - when I create my tree like this
Code:
model = create_notelist_model();
treeview = gtk_tree_view_new_with_model (model);


Do I need to unref or otherwise free the model variable?

If you'll be using that model throughout the application life cycle, there is no need to unref it. But if you'll be replacing treeview's model during application and want your model to be destroyed when replaced, then you need to call g_object_unref on you model after it's been added to the treeview.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 8:25 pm 
Offline
GTK+ Geek

Joined: Wed May 21, 2008 2:20 pm
Posts: 76
tadeboro wrote:
If the icon in need of displaying is not accessible using methods listed above, you may need to create a pixbuf from file and pack it into store. The simplest way of doing that is with gdk_pixbuf_new_from_file function. Then add pixbuf in your store and call g_object_unref on it once (this way you get rid of your own reference and the pixbuf will be automatically destroyed when removed from your model). More docs: http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-file-loading.html


Thanks too. Later on I'll probably use my own icons stored as external files, so I was also wondering whether I should free them or the model will take care of that.

By the way - which way is better - use GDK_TYPE_PIXBUF for the relevant column in my pixbuf and load images from file each time the model needs to be repopulated with data, or add my images to the stock somehow (is it possible at all?) and use the stock-id property?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 9:00 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
If you'll be using your pictures in treeview only, creating stock item for each of them may feel like an over-engineering. But if you intend to use your icons elsewhere in your application (on buttons, for example), the cost-benefit ratio just may tilt toward the stock items.

Oh, and yes, stock items can be created;-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group