<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Shared resources</title> <link rel="stylesheet" type="text/css" href="style.css"> <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> <link rel="home" href="index.html" title="Programming with gtkmm 3"> <link rel="up" href="chapter-memory.html" title="Chapter 25. Memory management"> <link rel="prev" href="chapter-memory.html" title="Chapter 25. Memory management"> <link rel="next" href="chapter-builder.html" title="Chapter 26. 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 25. 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"> <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::Pixbuf</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">Glib::RefPtr<></code> smartpointer. Cairomm has its own smartpointer, <code class="classname">Cairo::RefPtr<></code>. </p> <p> Objects such as <code class="classname">Gdk::Pixbuf</code> can only be instantiated with a <code class="methodname">create()</code> function. For instance, </p> <pre class="programlisting"> Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file(filename); </pre> <p> </p> <p> You have no way of getting a bare <code class="classname">Gdk::Pixbuf</code>. In the example, <code class="varname">pixbuf</code> is a smart pointer, so you can do this, much like a normal pointer: </p> <pre class="programlisting"> int width = 0; if(pixbuf) { width = pixbuf->get_width(); } </pre> <p> </p> <p> When <code class="varname">pixbuf</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::Pixbuf> pixbuf2 = pixbuf; </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" style="list-style-type: disc; "> <li class="listitem"><p> Bjarne Stroustrup, "The C++ Programming Language" Forth Edition - section 34.3 </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 25. 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 26. Glade and Gtk::Builder</td> </tr> </table> </div> </body> </html>