Re: Equivalent of the gtk g_object_ref/unref functions in gtkmm?



Murray Cumming wrote:
On Thu, 2007-03-22 at 16:49 +0100, Jef Driesen wrote:
When I look at some gtk C code, I see everywhere pointers to widgets (and other things). As i understand all gtk objects are reference counted to keep the objects alive until the last pointer is released.

How does that work in gtkmm? At first sight, gtkmm widgets doesn't seem to be reference counted, because something like this does not work (because of the private copy constructor):

Gtk::Button a, b(a);

Do I need to use smart pointers (e.g. boost::shared_ptr) for that? Does that means the underlying gtk reference counting mechanism is not used in gtkmm?

I read in the gtkmm documentation there is a Glib::RefPtr, that uses the internal gtk/gobject reference counting. If I understand that correctly, I can use this smart pointer (instead of third party implementation like boost)?

Glib::RefPtr<Gtk::Button> a(new Gtk::Button()), b(a);

But I've never seen that before, only with non-widgets objects like pixbufs and treemodels.

RefPtr is not for use with widgets. Widgets (or other things that derive
from GtkObject rather than GObject) have different, more complicated
memory management than the reference-counted objects such as TreeModel.
This page has an overview of gtkmm memory management:
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch22.html

In summary, it's just regular C++ memory management so you can do what
you like, and we have some things to help you sometimes.

Thanks for the clarification.

One of the problems I'm trying to solve is I want to create a treeview that will act as a sidebar pane in my application. And I would like to store a reference to other widgets (which are in a notebook) in the treemodel. When the user activates an item in the treeview, the corresponding widget should be displayed in the main application pane (by displaying the correct notebook page). How should this treemodel look like in gtkmm? Is a smart pointer going to interfere with the automatic memory management when I add the widget to the notebook? Do I need smart pointers at all for all this?

You can use some other smartpointer, such as one from boost, or you can
use some parent-child relationship of your own.

What do you mean with "parent-child relationship"?




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]