GNOME Bugzilla – Bug 89557
incorrect reference counting for gobject
Last modified: 2002-08-04 14:34:22 UTC
We have discovered some irregularities in the gobject reference counting when using objects of short live time, e.g. dialogs. The sample program shows the behavior by incresing the number of toplevel windows. This vanishs when we apply the silly workaround by defining WITH_UNREF. The real reason for this misbehavior is that in the destructor of Object (gtk/gtkmm/object.cc) the reference counter will not be decremented if gobj() equals NULL. But other methods as disconnect_cpp_wrapper() which is called by Object::destroy_ for instance set gobj() to NULL, so that at the end the destructor forgets to decrease the reference counter resulting in the shown resource leak. Below the example program and test output: $ cat test.cc #include <gtkmm/main.h> #include <gtkmm/window.h> #include <gtkmm/dialog.h> #include <gtkmm/button.h> #include <iostream> using namespace std; using namespace Gtk; using SigC::slot; class MyDialog : public Dialog { public: MyDialog() { add_button("Ok", 0); } MyDialog::~MyDialog() { #ifdef WITH_UNREF g_object_unref(G_OBJECT(gobj())); #endif } }; class MyWindow : public Window { public: MyWindow() { set_size_request(200, 200); Box* mainBox = manage(new HBox); Button* button = manage(new Button("Show Dialog")); button->signal_clicked().connect(slot(*this, &MyWindow::ShowDialog)); mainBox->pack_start(*button); add(*mainBox); } void ShowDialog() { { MyDialog d; d.run(); } list<Window*> toplevelwindows = list_toplevels(); cout << "toplevelwindows.size = " << toplevelwindows.size() << endl; } }; int main(int argc, char* argv[]) { Gtk::Main kit(argc, argv); MyWindow win; win.show_all(); kit.run(win); return 0; } $ ./test toplevelwindows.size = 2 toplevelwindows.size = 3 toplevelwindows.size = 4
May I add this code to the gtkmm tests directory?
Of course.
This seems to be fixed now, though some other stuff (examples/book/menus/main_menu, for example) now segfaults on close. Thanks for giving me a clean example.