Shared resources

Some objects, such as Gdk::Pixbufs and Pango::Fonts, are obtained from a shared store. Therefore you cannot instantiate your own instances. These classes typically inherit from Glib::Object. Rather than requiring you to reference and unreference these objects, gtkmm uses the Glib::RefPtr<> smartpointer. Cairomm has its own smartpointer, Cairo::RefPtr<>.

Objects such as Gdk::Pixbuf can only be instantiated with a create() function. For instance,

Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file(filename);

You have no way of getting a bare Gdk::Pixbuf. In the example, pixbuf is a smart pointer, so you can do this, much like a normal pointer:

int width = 0;
if(pixbuf)
{
  width = pixbuf->get_width();
}

When pixbuf goes out of scope an unref() will happen in the background and you don't need to worry about it anymore. There's no new so there's no delete.

If you copy a RefPtr, for instance

Glib::RefPtr<Gdk::Pixbuf> pixbuf2 = pixbuf;

, or if you pass it as a method argument or a return type, then RefPtr will do any necessary referencing to ensure that the instance will not be destroyed until the last RefPtr has gone out of scope.

See the appendix for detailed information about RefPtr.

If you wish to learn more about smartpointers, you might look in these books: