Re: Correct way to delete widgets? (Memleak)




Richard Fish <rjf@estinc.com> writes:

> Consider the following code:
> 
> #include <gtk/gtk.h>
> int main(int argc, char **argv) {
>   int i;
>   GtkWidget * l;
> 
>   gtk_init(&argc,&argv);
> 
>   for (i=0; i<100000; i++) {
>     l=gtk_label_new("Hello");
>     gtk_object_unref((GtkObject *)l);
>     l=gtk_window_new(GTK_WINDOW_TOPLEVEL);
>     gtk_object_destroy((GtkObject *)l);
>   }
> 
>   exit(0);
> }
> 
> This code compiles and runs succesfully, with no memory leaks.  However,
> if I change the gtk_object_unref to gtk_object_destroy, I get a memory
> leak, because the ref count never makes it to 0.  On the other hand, if
> I change the gtk_object_destroy to gtk_object_unref, I get an assertion
> error in gtk_object_unref because the reference count has dropped too
> low.
> 
> I'm assuming this is a bug...it doesn't make sense to have to use
> different deletion methods depending upon the widget you are dealing
> with.  But which one is broken?

See docs/refcounting.txt. It's a little technical, but it
should explain why the above works as it does.

Regards,
                                        Owen



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