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 772859 - [patch x7] Fix memory leaks in implementations of common widgets
[patch x7] Fix memory leaks in implementations of common widgets
Status: RESOLVED OBSOLETE
Product: gtk+
Classification: Platform
Component: .General
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtk-bugs
gtk-bugs
: 772598 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2016-10-13 12:58 UTC by Alan Jenkins
Modified: 2018-05-02 17:36 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
[PATCH 1/7] applicationwindow: fix leak of help_overlay (2.26 KB, patch)
2016-10-13 12:59 UTC, Alan Jenkins
committed Details | Review
[PATCH 2/7] shortcutswindow: fix leak of internal widgets (2.95 KB, patch)
2016-10-13 13:00 UTC, Alan Jenkins
none Details | Review
[PATCH 3/7] shortcutsgroup: fix leak of title (1.60 KB, patch)
2016-10-13 13:01 UTC, Alan Jenkins
committed Details | Review
[PATCH 4/7] headerbar: fix leak of start_box/end_box (1.29 KB, patch)
2016-10-13 13:01 UTC, Alan Jenkins
committed Details | Review
[PATCH 5/7] headerbar: fix leak of separator (1.75 KB, patch)
2016-10-13 13:02 UTC, Alan Jenkins
committed Details | Review
[PATCH 6/7] headerbar: fix leak of label_sizing_box (1.43 KB, patch)
2016-10-13 13:02 UTC, Alan Jenkins
committed Details | Review
[PATCH 7/7] scrolledwindow: fix leak of pan_gesture (811 bytes, patch)
2016-10-13 13:02 UTC, Alan Jenkins
committed Details | Review

Description Alan Jenkins 2016-10-13 12:58:42 UTC
GTK causes Nautilus to leak over 1MB per window (#772598).  (Technically "still reachable" instead of leaked, because of how GTK manages memory).

With this patch series, per-window leakage falls to about 100kB.  I expect the exact figures are variable, but it's a notable difference.
Comment 1 Alan Jenkins 2016-10-13 12:59:58 UTC
Created attachment 337599 [details] [review]
[PATCH 1/7] applicationwindow: fix leak of help_overlay
Comment 2 Alan Jenkins 2016-10-13 13:00:37 UTC
Created attachment 337600 [details] [review]
[PATCH 2/7] shortcutswindow: fix leak of internal widgets
Comment 3 Alan Jenkins 2016-10-13 13:01:13 UTC
Created attachment 337601 [details] [review]
[PATCH 3/7] shortcutsgroup: fix leak of title
Comment 4 Alan Jenkins 2016-10-13 13:01:45 UTC
Created attachment 337602 [details] [review]
[PATCH 4/7] headerbar: fix leak of start_box/end_box
Comment 5 Alan Jenkins 2016-10-13 13:02:16 UTC
Created attachment 337603 [details] [review]
[PATCH 5/7] headerbar: fix leak of separator
Comment 6 Alan Jenkins 2016-10-13 13:02:40 UTC
Created attachment 337604 [details] [review]
[PATCH 6/7] headerbar: fix leak of label_sizing_box
Comment 7 Alan Jenkins 2016-10-13 13:02:59 UTC
Created attachment 337605 [details] [review]
[PATCH 7/7] scrolledwindow: fix leak of pan_gesture
Comment 8 Matthias Clasen 2016-10-13 13:10:05 UTC
Review of attachment 337599 [details] [review]:

Looks right. Just a few coding style niggles: we put a space before ( in function calls.
Comment 9 Matthias Clasen 2016-10-13 13:14:05 UTC
Review of attachment 337600 [details] [review]:

this will need very careful scrutiny. The whole container setup for the shortcuts widgets is such a disaster
Comment 10 Matthias Clasen 2016-10-13 13:14:57 UTC
Review of attachment 337601 [details] [review]:

ok
Comment 11 Matthias Clasen 2016-10-13 13:15:47 UTC
Review of attachment 337602 [details] [review]:

ok
Comment 12 Matthias Clasen 2016-10-13 13:19:01 UTC
Review of attachment 337603 [details] [review]:

ok
Comment 13 Matthias Clasen 2016-10-13 13:20:46 UTC
Review of attachment 337604 [details] [review]:

ok. Again, please put a space before ( in function calls
Comment 14 Matthias Clasen 2016-10-13 13:21:43 UTC
Review of attachment 337605 [details] [review]:

sure
Comment 15 Alan Jenkins 2016-10-13 15:12:11 UTC
Review of attachment 337600 [details] [review]:

thx for fixups :) (other patches).

I agree it is a hard nut to crack.  Sadly this one must be what saves the most memory (~ 800kB).

At least there's no dynamic variation in the lifetimes.  It just goes _init -> _add(widgets) -> _dispose -> _remove(widgets).  And it passed valgrind for memory-safety.

There are some possible cleanups.  Equal scrutiny required, just that it might help to list different options.

* exclude dangling pointers by carpet-bombing with weak_pointer(), so any bugs would give nice obvious segfaults.
* don't call _dispose() -> widget_destroy() -> _remove().  Instead, have _dispose() call parent_class->remove().
(could do both of these)

or

* remove override of forall(), remove(), and add().  Override gtk_buildable_add_child() instead.  Using GtkBuilder is "the recommended way", and the _add() behaviour is not explicitly documented.
Comment 16 Daniel Boles 2016-10-13 19:05:07 UTC
(In reply to Alan Jenkins from comment #15)
>
> * remove override of forall(), remove(), and add().  Override
> gtk_buildable_add_child() instead.  Using GtkBuilder is "the recommended
> way", and the _add() behaviour is not explicitly documented.

Uh, please, no. Any fix for the leak should be done properly, not by removing people's ability to programmatically create these widgets, just for an easy ride.

Specifically, there are those of us who don't normally use builder but still want to use GtkShortcuts*. For that case, Benjamin wants to add proper API for these widgets - to replace the current requirement to use raw GObject calls - saying it seems really wrong that it's like this in the first place

I've opened a BZ about the lack of API:
https://bugzilla.gnome.org/show_bug.cgi?id=770377
Comment 17 Alan Jenkins 2016-10-13 19:59:36 UTC
(In reply to djb from comment #16)
> (In reply to Alan Jenkins from comment #15)
> >
> > * remove override of forall(), remove(), and add().  Override
> > gtk_buildable_add_child() instead.  Using GtkBuilder is "the recommended
> > way", and the _add() behaviour is not explicitly documented.
> 
> Uh, please, no.

> Specifically, there are those of us who don't normally use builder but still
> want to use GtkShortcuts*. For that case, Benjamin wants to add proper API
> for these widgets - to replace the current requirement to use raw GObject
> calls - saying it seems really wrong that it's like this in the first place

Your scrutiny is appreciated :).

I did consider something, just like gtk_places_sidebar_add_shortcut().  That _would_ be an API.  The problem is narrower: it wouldn't be backwards compatible.  But I see you mention _existing_ code that doesn't use GtkBuilder :(.

I suspect this approach would also fix the issue you found, that show_all() isn't working as normal.  (I'm not sure your explanations are consistent - if show_all() doesn't show the sections, then why would you not need to show the second section?  And I know the overridden foreach() is freaky, but iterating the sections is exactly what it's supposed to do.  So I'm kind of scared to know why show_all() doesn't do the job).

One way to notice the "disaster" of shortcutswindow implementation, is that you're not allowed to write it outside of GTK.  gtkcontainer literally has a macro called SPECIAL_CONTAINER to identify the "containers inside GTK+ [which] are cheating".  

And so, it ends up needing this "very special scrutiny".  Because it's not enough to understand the public GTK api; you need to reason about what happens when you're "cheating".
Comment 18 Daniel Boles 2016-10-13 20:17:03 UTC
(In reply to Alan Jenkins from comment #17)
>
> Your scrutiny is appreciated :).

Likewise, I love seeing leaks getting plugged. :D


> I did consider something, just like gtk_places_sidebar_add_shortcut().  That
> _would_ be an API.  The problem is narrower: it wouldn't be backwards
> compatible.  But I see you mention _existing_ code that doesn't use
> GtkBuilder :(.

Pitivi has user-settable accelerators and (via the Python bindings) generates GtkShortcuts* widgets at runtime, which seems like a significant existing use.

I've only built proof-of-concepts using these widgets - partly because the lack of API made me nervous about their stability. However, I have code that would be perfectly paired up with dynamically generated Shortcuts widgets, so I'd like to be able to do it with guaranteed stable API and consistent code style.


> I suspect this approach would also fix the issue you found, that show_all()
> isn't working as normal.  (I'm not sure your explanations are consistent -
> if show_all() doesn't show the sections, then why would you not need to show
> the second section?  And I know the overridden foreach() is freaky, but
> iterating the sections is exactly what it's supposed to do.  So I'm kind of
> scared to know why show_all() doesn't do the job).

It was Matthias's explanation, not mine. He said the ShortcutsWindow takes over visibility management but respects the initial setting of added children. My interpretation is that the Window can newly show Section if it's switched-to from _another_ Section - but if the Window is opened with _no_ Sections visible, it doesn't show a default Section, so there's nothing to switch from.

That said, I'm not so clear on the LTR vs RTL explanation about why this can't be done, but then I know next to nothing about i18n (only that Christian has recently changed things so that accelerator labels are always shown as LTR).


> One way to notice the "disaster" of shortcutswindow implementation, is that
> you're not allowed to write it outside of GTK.  gtkcontainer literally has a
> macro called SPECIAL_CONTAINER to identify the "containers inside GTK+
> [which] are cheating".  
> 
> And so, it ends up needing this "very special scrutiny".  Because it's not
> enough to understand the public GTK api; you need to reason about what
> happens when you're "cheating".

I haven't looked into the widgets in this much depth. I guess if I ever have time to try my hand at a patch for that ticket, I'd find out. I suspect it's a bit over my head at this point, though.
Comment 19 Alan Jenkins 2017-10-23 11:33:19 UTC
*** Bug 772598 has been marked as a duplicate of this bug. ***
Comment 20 GNOME Infrastructure Team 2018-05-02 17:36:46 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/gtk/issues/684.