Problematic use of Gtk.Builder in examples and demos
Submitted by lovetox
The Gtk.Builder demo/example in the repo
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()