Re: memory leak (continuation)



Hi,

William Dulyea <wdulyea qualcomm com> writes:

> An expansion to an earlier mail I sent... In this code snippet below I
> can observ that removing(destroying) container widgets does not have
> the desired effect. Please comment:>
> 
> Code snippet...........................................
> cur_page = gtk_notebook_get_current_page( notebook );
> fprintf( stderr, "on_notebook_switch_page,switching from:%i to:%i\n",
> cur_page, page_n );
> 
> child = gtk_container_children(GTK_CONTAINER(notebookpage[cur_page].widget));
> while( child ) {
>    GtkWidget *widget;
> 
>    widget = GTK_WIDGET(child->data);
>    gtk_container_remove(
>    GTK_CONTAINER(notebookpages[cur_page].widget),widget );

whoahh, this is cruel, you are removing the widget from the container
while iterating over the list of container children. 
> 
>    fprintf( stderr, "on_notebook_switch_page,destroy widget:%p", widget);
>    gtk_widget_destroy( widget );

>    if( widget )
>      fprintf( stderr, " widget still referenced:%p\n", widget);
>    else
>      fprintf( stderr, "\n" );

what are you tring to proof here? You called gtk_widget_destroy() on
the widget. What makes you think the widget pointer gets nullified?
gtk_widget_destroy() does not even know the location of the memory
you use to hold a pointer on the widget. How should it nullify it?
Your pointer will keep its value, it only points to invalid memory
now.

You could of course set up a signal handler to nullify the pointer on
widget destruction:

 gtk_signal_connect_data (GTK_WIDGET (widget), "destroy",
                          GTK_SIGNAL_FUNC (g_nullify_pointer), 
                          &widget);

Hmm, g_nullify_pointer is glib-2.0, but I think you can imagine what 
it does.

To make things worse, you now dereference the child you have removed
above. With a little luck this works since GList structures are
allocated from a pool and won't be freed. Perhaps that's the memleak
you observe (I didn't check your previous mails)?

>    child = child->next;
> }
> create_page_item( notebook, page_n );


Salut, Sven



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