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 89557 - incorrect reference counting for gobject
incorrect reference counting for gobject
Status: RESOLVED FIXED
Product: gtkmm
Classification: Bindings
Component: general
2.0
Other All
: Normal normal
: ---
Assigned To: gtkmm-forge
gtkmm-forge
Depends on:
Blocks:
 
 
Reported: 2002-07-31 19:36 UTC by Michael v. Szombathely
Modified: 2002-08-04 14:34 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Michael v. Szombathely 2002-07-31 19:36:11 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
Comment 1 Murray Cumming 2002-08-01 18:51:30 UTC
May I add this code to the gtkmm tests directory?
Comment 2 Michael v. Szombathely 2002-08-01 18:54:24 UTC
Of course.
Comment 3 Murray Cumming 2002-08-04 14:34:22 UTC
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.