<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Shared resources</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Programming with gtkmm"> <link rel="up" href="chapter-memory.html" title="Chapter 22. Memory management"> <link rel="prev" href="chapter-memory.html" title="Chapter 22. Memory management"> <link rel="next" href="chapter-builder.html" title="Chapter 23. Glade and Gtk::Builder"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Shared resources</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="chapter-memory.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center">Chapter 22. Memory management</th> <td width="20%" align="right"> <a accesskey="n" href="chapter-builder.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="sect1" title="Shared resources"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-memory-shared-resources"></a>Shared resources</h2></div></div></div> <p> Some objects, such as <code class="classname">Gdk::Pixmap</code>s and <code class="classname">Pango::Font</code>s, are obtained from a shared store. Therefore you cannot instantiate your own instances. These classes typically inherit from <code class="classname">Glib::Object</code>. Rather than requiring you to reference and unreference these objects, <span class="application">gtkmm</span> uses the <code class="classname">RefPtr<></code> smartpointer. </p> <p> Objects such as <code class="classname">Gdk::Bitmap</code> can only be instantiated with a <code class="methodname">create()</code> function. For instance, </p> <pre class="programlisting"> Glib::RefPtr<Gdk::Bitmap> bitmap = Gdk::Bitmap::create(window, data, width, height); </pre> <p> </p> <p> You have no way of getting a bare <code class="classname">Gdk::Bitmap</code>. In the example, <code class="varname">bitmap</code> is a smart pointer, so you can do this, much like a normal pointer: </p> <pre class="programlisting"> if(bitmap) { int depth = bitmap->get_depth(). } </pre> <p> </p> <p> When <code class="varname">bitmap</code> goes out of scope an <code class="methodname">unref()</code> will happen in the background and you don't need to worry about it anymore. There's no <code class="literal">new</code> so there's no <code class="literal">delete</code>. </p> <p> If you copy a <code class="classname">RefPtr</code>, for instance </p> <pre class="programlisting"> Glib::RefPtr<Gdk::Bitmap> bitmap2 = bitmap. </pre> <p> , or if you pass it as a method argument or a return type, then <code class="classname">RefPtr</code> will do any necessary referencing to ensure that the instance will not be destroyed until the last <code class="classname">RefPtr</code> has gone out of scope. </p> <p>See the <a class="link" href="chapter-refptr.html" title="Appendix A. The RefPtr smartpointer">appendix</a> for detailed information about RefPtr.</p> <p> If you wish to learn more about smartpointers, you might look in these books: </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem"><p> Bjarne Stroustrup, "The C++ Programming Language" - section 14.4.2 </p></li> <li class="listitem"><p> Nicolai M. Josuttis, "The C++ Standard Library" - section 4.2 </p></li> </ul></div> <p> </p> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="chapter-memory.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"><a accesskey="u" href="chapter-memory.html"><img src="icons/up.png" alt="Up"></a></td> <td width="40%" align="right"> <a accesskey="n" href="chapter-builder.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Chapter 22. Memory management </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> Chapter 23. Glade and Gtk::Builder</td> </tr> </table> </div> </body> </html>