After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 344858 - gtk_menu_item_new_with_label() created widget with refcount 2
gtk_menu_item_new_with_label() created widget with refcount 2
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Widget: GtkMenu
2.8.x
Other Linux
: Normal normal
: ---
Assigned To: gtk-bugs
gtk-bugs
: 376443 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2006-06-14 09:15 UTC by Ross Burton
Modified: 2011-08-24 12:17 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Test program (845 bytes, text/x-csrc)
2010-11-05 08:59 UTC, L. López
  Details
Patch using weak refs (1.77 KB, patch)
2010-11-05 09:00 UTC, L. López
accepted-commit_now Details | Review

Description Ross Burton 2006-06-14 09:15:42 UTC
This code:

  m = gtk_menu_item_new ();
  g_print ("gtk_menu_item_new has %d references\n", G_OBJECT (m)->ref_count);
  m = gtk_menu_item_new_with_label ("foo");
  g_print ("gtk_menu_item_new_with_label has %d references\n", G_OBJECT (m)->ref_count);

Produces:

gtk_menu_item_new has 1 references
gtk_menu_item_new_with_label has 2 references

The second reference is due to the accel label holding a reference on its parent (the menu item itself).  However won't this stop the idiom of gtk_container_remove (menu, menuitem) destroying the widget unless the programmer explicitly kept a reference from working?
Comment 1 Tommi Komulainen 2006-06-14 09:24:10 UTC
Basically, this leaks depending on how the item is created, if it's with_label() it leaks, if it's created with gtk_menu_item_new() it doesn't.

item = gtk_menu_item_new_with_label ("foo");
gtk_container_add (GTK_CONTAINER(menu), item);
gtk_container_remove (GTK_CONTAINER(menu), item);

Maybe accel_label should use weak refs?
Comment 2 Ross Burton 2006-06-14 09:35:34 UTC
I should clarify that when I said "gtk_container_remove (menu, menuitem) destroying the widget" I meant the idiom of the containing unrefing the widget and thus reducing the refcount to 0, and causing finalisation.  I don't mean explicity gtk_widget_destroy().
Comment 3 Tim Janik 2006-06-14 09:41:15 UTC
using weak references for accel labels sounds reasonable.
are you going to cook up a patch for this?
note that the accel closure has to be properly disconencted/unset when the accel wiodget vanishes.
Comment 4 Tommi Komulainen 2006-06-14 09:54:19 UTC
Not immediately, no. I think for our case using gtk_widget_destroy() is semantically more correct and will the issue, for now.
Comment 5 Emmanuele Bassi (:ebassi) 2007-04-16 12:04:04 UTC
*** Bug 376443 has been marked as a duplicate of this bug. ***
Comment 6 L. López 2010-11-05 08:58:53 UTC
This bug still exists in gtk+-3.0, it can be checked with the test attached.

I see this bug marked as gnome-love bug and I want to try to fix it :)

If I understood correcty I should move g_signal_handlers_disconnect_by_func to a GWeakNotify callback and removing the _ref/_unref  thing, right?

A patch, against gtk+-3 master branch, implementing it is attached.
Comment 7 L. López 2010-11-05 08:59:42 UTC
Created attachment 173865 [details]
Test program
Comment 8 L. López 2010-11-05 09:00:44 UTC
Created attachment 173866 [details] [review]
Patch using weak refs
Comment 9 Matthias Clasen 2011-06-01 04:31:52 UTC
Review of attachment 173866 [details] [review]:

Patch looks reasonable to me.
Comment 10 L. López 2011-06-01 17:03:01 UTC
(In reply to comment #9)
> Review of attachment 173866 [details] [review]:
> 
> Patch looks reasonable to me.

FYI, the patch still works on gtk+ master (with two hunks, but it is valid).