Re: memprof and GTK/GNOME functions




Andy Kahn <ackahn@netapp.com> writes:

> Hi Owen,
> 
> Just tried your MemProf utility and had some questions/comments.  Upon
> using MemProf with my GTK/GNOME application, MemProf found two leaks
> in my code: one with gtk_style_new(), and one in my routine.  I don't
> think gtk_style_new() is at fault, but here are the code snippets for
> both cases:
> 
>   [1]
> 	style = gtk_style_new();	/* MemProf points at this line */
> 	gtk_widget_set_style(GTK_WIDGET(d->data), style);
> 	gtk_widget_set_rc_style(GTK_WIDGET(d->data));
> 	gtk_widget_ensure_style(GTK_WIDGET(d->data));
> 
>   [2]
> 	GnomeUIInfo *menuitem;
> 
> 	menuitem = g_new0(GnomeUIInfo, 2);	/* MemProf points here */
> 	... [ filling in fields for menuitem ] ...
> 	gnome_app_insert_menus_with_data(GNOME_APP(w->toplev),
> 					 path, menuitem, data);
> 
> 
> So a general question: how accurate is MemProf?

It's pretty accurate in not returning false positives - saying memory
is leaked when it is not; To cause this, you'd have to go to some
trouble to disguise a pointer. Say by:

 - XOR'ing pointers in doubly linked lists
 - Storing pointers non-aligned
 - Storing pointers that point outside of the allocated memory
   (Something like: char *a = malloc(50) - 1;)

All of these things probably should be avoided for other reasons
 
> A general comment: from looking at the API, and even doing cursory
> inspections of the GTK and GNOME code, it's not apparent that what my
> code is doing up above is considered a memory leak.

Yes, both of those are leaks.

  - gtk_widget_set_style() adds an additional refcount to the style.
  - gnome_app_insert_menus_with_data() doesn't store the array that
     was passed in.

[ BTW, the call to gtk-widget_set_rc_style() is completely useless 
  and gtk-widget_set_rc_style() is basically an internal function. ]

> Does MemProf know something that I don't about GTK and GNOME?

Yep. ;-)
 
Actually, its a fairly general principle that GTK+ and GNOME functions
pretty much never take ownership of memory that is passed in - they
either copy or add a refcount if they need to retain ownership.

So, I'd generally expect only exceptions to this rule to be documented.

> Both of the "leaks" I mentioned above were fixed(?).  The first by
> calling gtk_style_unref(), and the second by freeing the variable
> "menuitem".  There doesn't appear to be any problems with my
> application after doing this, but is there some way to be sure?

Freeing memory to eagerly is usually more detectable that not freeing it -
it generally causes a crash.
 
> Overall, aside from the lack of documentation for GTK/GNOME (there's
> that old problem again), MemProf seems to be a fairly useful tool.

Thanks! 
                                        Owen



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