From: jcore@... Date: 2001-11-08T10:23:29+09:00 Subject: [ruby-talk:24621] ruby-gtk reference counting I'm new to ruby-gtk (and Ruby for that matter) and I wonder why ruby-gtk doesn't allow the programmer to do any reference counting. Was this a deliberate design decision? Here's the situation that I have where I need to manually access the reference count of an object... I have a variable list of hboxes packed into a vbox containing notebook widgets. When notebooks are added/removed in order to maintain the tiled layout I destroy all of the hboxes and recreate new ones. The only problem is that when the notebooks are removed from the hboxes they are dereferenced and destroyed also. Here's the relevant code: def repack @connection_boxes.each {|b| b.each {|w| # w.ref b.remove(w) } @vbox.remove(b) } @connection_boxes.clear dim = Math.sqrt(@visible_connections.size).ceil pos = 0 dim.times { box = Gtk::HBox.new(true, 0) @vbox.pack_start(box) @connection_boxes << box box.show } dim.times { # x direction 0.upto(dim - 1) {|n| # y direction unless pos >= @visible_connections.size @connection_boxes[n].pack_start(@visible_connections[pos], true, true, 0) # @visible_connections[pos].unref pos += 1 end } } end This code will fail with a message about Gtk::Object already being destroyed. My current solution for this is to patch ruby-gtk to allow ref and unref calls to be made on an Object. Is there a better way to do this? I would appreciate any help on the subject and I've also included the patch that I made for the time being. diff -ru gtk-0.25/src/rbgtkobject.c gtk-0.25.1/src/rbgtkobject.c --- gtk-0.25/src/rbgtkobject.c Sun Mar 4 04:22:36 2001 +++ gtk-0.25.1/src/rbgtkobject.c Mon Nov 5 13:00:25 2001 @@ -497,6 +497,20 @@ rb_raise(rb_eTypeError, "can't clone %s", rb_class2name(CLASS_OF(self))); } +static VALUE +gobj_ref(self) + VALUE self; +{ + gtk_object_ref(get_gobject(self)); +} + +static VALUE +gobj_unref(self) + VALUE self; +{ + gtk_object_unref(get_gobject(self)); +} + void Init_gtk_object() { gObject = rb_define_class_under(mGtk, "Object", rb_cObject); @@ -551,6 +565,8 @@ rb_define_method(gObject, "==", gobj_equal, 1); rb_define_method(gObject, "inspect", gobj_inspect, 0); rb_define_method(gObject, "clone", gobj_clone, 0); + rb_define_method(gObject, "ref", gobj_ref, 0); + rb_define_method(gObject, "unref", gobj_unref, 0); /* child initialize */ Init_gtk_data();