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 784728 - Problematic use of Gtk.Builder in examples and demos
Problematic use of Gtk.Builder in examples and demos
Status: RESOLVED OBSOLETE
Product: pygobject
Classification: Bindings
Component: gobject
3.24.x
Other All
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2017-07-09 21:52 UTC by lovetox
Modified: 2018-01-10 20:59 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description lovetox 2017-07-09 21:52:21 UTC
The Gtk.Builder demo/example in the repo

https://github.com/GNOME/pygobject/blob/master/demos/gtk-demo/demos/builder.py

in this example a reference is set to the Gtk.Builder object on self, then with connect_signals(self), Gtk.Builder gets a reference of BuilderApp.
I dont know what Gtk.Builder exactly does with that reference but from my tests it creates a ref cylce that it seems python can not detect.

Hence neither Gtk.Builder nor BuilderApp is garbage collected ever.

Just dont quit Gtk on destroy, and create a list with some million entrys on the class.

if you run this and close the window, the class is never garbage collected because Gtk.Builder holds somewhere a reference to it. The memory is not released.

If you run the same example but comment out
"self.builder.connect_signals(self)"
the class is garbage collected.

Im aware that this could be considered not a bug.
But then we should at least hint somewhere in the documentation or in the examples that this ref cycle has to be broken somehow.

Im curious how other devs handle this, or if they are aware of it.

Here my test:

***
import os

from gi.repository import Gtk


class BuilderApp:
    def __init__(self, demoapp):
        self.demoapp = demoapp

        self.builder = Gtk.Builder()
        if demoapp is None:
            filename = os.path.join('data', 'demo.ui')
        else:
            filename = demoapp.find_file('demo.ui')

        self.builder.add_from_file(filename)
        self.builder.connect_signals(self)

        window = self.builder.get_object('window1')
        window.connect('destroy', self.quit_activate)
        window.show_all()
        self.list_ = []
        for x in range(10000000):
            self.list_.append('test')


    def about_activate(self, action):
        about_dlg = self.builder.get_object('aboutdialog1')
        about_dlg.run()
        about_dlg.hide()

    def quit_activate(self, action):
        return


def main(demoapp=None):
    BuilderApp(demoapp)
    Gtk.main()

if __name__ == '__main__':
    main()
***
Comment 1 GNOME Infrastructure Team 2018-01-10 20:59:15 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/pygobject/issues/136.