GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Sep 20, 2014 12:00 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GtkListStore - fill, refill and recreate.
PostPosted: Thu Aug 02, 2012 5:24 am 
Offline
Familiar Face

Joined: Fri Mar 25, 2011 7:55 am
Posts: 35
Location: romania
Hi,

I have the following code, I want to know if the idea is correct.
Calling view_and_model with new_view = TRUE the list is cleared and the new list is created in view_box.
Calling view_and_model with new_view = FALSE the list is cleared from view_box.
Any problems with memory allocation ?
After calling "gtk_widget_destroy( scrolled_win )" all objects are cleared from RAM or not ?
This style code is working in a aplication but I'm not sure about clearing RAM after "gtk_widget_destroy( scrolled_win )".

Thanks !

Code:
GtkWidget     *scrolled_win = NULL;
GtkWidget     *view;
GtkListStore  *store_details;

static void fill( void ) {
.....
}

static void fill_model( void ) {
   store_details = gtk_list_store_new( 1, G_TYPE_STRING );
// Inserted code !!!!!
   gtk_tree_view_set_model( GTK_TREE_VIEW( view ), GTK_TREE_MODEL( store_details ));
// ----------------
   fill();
}

static void view_and_model( GtkWidget *view_box,
                            gboolean   new_view ) {           
   GtkCellRenderer   *renderer;
   GtkTreeViewColumn *column;
   
   if ( new_view ) {
      if ( scrolled_win != NULL ) {
         gtk_widget_destroy( scrolled_win );
//       Inserted code !!!!
         g_object_unref( G_OBJECT( store_details ));
//       -----------------
      }
      view = gtk_tree_view_new();
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes( "col", renderer, "text", 0, NULL );
      fill_model();
      scrolled_win = gtk_scrolled_window_new( NULL, NULL );
      gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
      gtk_container_add( GTK_CONTAINER( scrolled_win ), view );
      gtk_box_pack_start( GTK_BOX( view_box ), scrolled_win, TRUE, TRUE, 0 );
      gtk_tree_view_expand_all( GTK_TREE_VIEW( view ));
      gtk_widget_show_all( view_box );
   }
   if ( !new_view ) {
      if ( scrolled_win != NULL ) {
    gtk_widget_destroy( scrolled_win );
//       Inserted code !!!!
         g_object_unref( G_OBJECT( store_details ));
//       -----------------
    scrolled_win = NULL;
      }
   }
}

_________________
c/c++ for ever


Last edited by perosoft on Thu Aug 02, 2012 9:06 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: GtkListStore - fill, refill and recreate.
PostPosted: Thu Aug 02, 2012 8:40 am 
Offline
Never Seen the Sunlight

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

gtk_widget_destroy() will clear any memory allocated by your widgets, which means you are safe here. But there is a potential leak with your data model. You don't show all of the steps for creating/filling the model, but most likely you are leaking entire model (GtkListStore along with it's data). Are you calling g_object_unref() on your model at any time?

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: GtkListStore - fill, refill and recreate.
PostPosted: Thu Aug 02, 2012 9:08 am 
Offline
Familiar Face

Joined: Fri Mar 25, 2011 7:55 am
Posts: 35
Location: romania
tadeboro wrote:
Hi.

gtk_widget_destroy() will clear any memory allocated by your widgets, which means you are safe here. But there is a potential leak with your data model. You don't show all of the steps for creating/filling the model, but most likely you are leaking entire model (GtkListStore along with it's data). Are you calling g_object_unref() on your model at any time?

Cheers,
Tadej


You have right, "store_details" still allocated after destroy "scrolled_win".
Code is modified now.
Hope is fine now.

thanks @Tadej.

_________________
c/c++ for ever


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest


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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group