Shared resources
| Chapter 24. Memory management |

## Shared resources

Some objects, such as `Gdk::Pixmap`s and `Pango::Font`s, 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 `RefPtr<>` smartpointer.

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

    Glib::RefPtr<Gdk::Bitmap> bitmap = Gdk::Bitmap::create(window, data, width, height);

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

    if(bitmap)
    {
      int depth = bitmap->get_depth().
    }

When `bitmap` 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::Bitmap> bitmap2 = bitmap.

, 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:

- Bjarne Stroustrup, "The C++ Programming Language" - section 14.4.2

- Nicolai M. Josuttis, "The C++ Standard Library" - section 4.2

Chapter 24. Memory management |  | Chapter 25. Glade and Gtk::Builder