<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 22. Memory management</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="index.html" title="Programming with gtkmm"> <link rel="prev" href="sec-idle-functions.html" title="Idle Functions"> <link rel="next" href="sec-memory-shared-resources.html" title="Shared resources"> </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">Chapter 22. Memory management</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="sec-idle-functions.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center"> </th> <td width="20%" align="right"> <a accesskey="n" href="sec-memory-shared-resources.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="chapter" title="Chapter 22. Memory management"> <div class="titlepage"><div><div><h2 class="title"> <a name="chapter-memory"></a>Chapter 22. Memory management</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <ul> <li><span class="sect1"><a href="chapter-memory.html#sec-memory-widgets">Widgets</a></span></li> <li><span class="sect1"><a href="sec-memory-shared-resources.html">Shared resources</a></span></li> </ul> </div> <div class="sect1" title="Widgets"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-memory-widgets"></a>Widgets</h2></div></div></div> <div class="sect2" title="Normal C++ memory management"> <div class="titlepage"><div><div><h3 class="title"> <a name="memory-normal"></a>Normal C++ memory management</h3></div></div></div> <p> <span class="application">gtkmm</span> allows the programmer to control the lifetime (that is, the construction and destruction) of any widget in the same manner as any other C++ object. This flexibility allows you to use <code class="literal">new</code> and <code class="literal">delete</code> to create and destroy objects dynamically or to use regular class members (that are destroyed automatically when the class is destroyed) or to use local instances (that are destroyed when the instance goes out of scope). This flexibility is not present in some C++ GUI toolkits, which restrict the programmer to only a subset of C++'s memory management features. </p> <p>Here are some examples of normal C++ memory management:</p> <div class="sect3" title="Class Scope widgets"> <div class="titlepage"><div><div><h4 class="title"> <a name="memory-class-scope"></a>Class Scope widgets</h4></div></div></div> <p> If a programmer does not need dynamic memory allocation, automatic widgets in class scope may be used. One advantage of automatic widgets in class scope is that memory management is grouped in one place. The programmer does not risk memory leaks from failing to <code class="literal">delete</code> a widget. </p> <p> The primary disadvantages of using class scope widgets are revealing the class implementation rather than the class interface in the class header. Class scope widgets also require Automatic widgets in class scope suffer the same disadvantages as any other class scope automatic variable. </p> <p> </p> <pre class="programlisting"> #include <gtkmm/button.h> class Foo { private: Gtk::Button theButton; // will be destroyed when the Foo object is destroyed }; </pre> <p> </p> </div> <div class="sect3" title="Function scope widgets"> <div class="titlepage"><div><div><h4 class="title"> <a name="memory-function-scope"></a>Function scope widgets</h4></div></div></div> <p> If a programmer does not need a class scope widget, a function scope widget may also be used. The advantages to function scope over class scope are the increased data hiding and reduced dependencies. </p> <pre class="programlisting"> { Gtk::Button aButton; aButton.show(); ... kit.run(); } </pre> <p> </p> </div> <div class="sect3" title="Dynamic allocation with new and delete"> <div class="titlepage"><div><div><h4 class="title"> <a name="memory-dynamic-allocation"></a>Dynamic allocation with new and delete</h4></div></div></div> <p> Although, in most cases, the programmer will prefer to allow containers to automatically destroy their children using <code class="function">manage()</code> (see below), the programmer is not required to use <code class="function">manage()</code>. The traditional <code class="literal">new</code> and <code class="literal">delete</code> operators may also be used. </p> <p> </p> <pre class="programlisting"> Gtk::Button* pButton = new Gtk::Button("Test"); // do something useful with pButton delete pButton; </pre> <p> Here, the programmer deletes pButton to prevent a memory leak. </p> </div> </div> <div class="sect2" title="Managed Widgets"> <div class="titlepage"><div><div><h3 class="title"> <a name="memory-managed-widgets"></a>Managed Widgets</h3></div></div></div> <p> Alternatively, you can let a widget's container control when the widget is destroyed. In most cases, you want a widget to last only as long as the container it is in. To delegate the management of a widget's lifetime to its container, first create it with <code class="function">manage()</code> and pack it into its container with <code class="methodname">add()</code>. Now, the widget will be destroyed whenever its container is destroyed. </p> <div class="sect3" title="Dynamic allocation with manage() and add()"> <div class="titlepage"><div><div><h4 class="title"> <a name="memory-managed-dynamic"></a>Dynamic allocation with manage() and add()</h4></div></div></div> <p> <span class="application">gtkmm</span> provides the <code class="function">manage()</code> function and <code class="methodname">add()</code> methods to create and destroy widgets. Every widget except a top-level window must be added or packed into a container in order to be displayed. The <code class="function">manage()</code> function marks a packed widget so that when the widget is added to a container, the container becomes responsible for deleting the widget. </p> <p> </p> <pre class="programlisting"> MyWidget::MyWidget() { Gtk::Button* pButton = manage(new Gtk::Button("Test")); add(*pButton); //add aButton to MyWidget } </pre> <p> Now, when objects of type <code class="classname">MyWidget</code> are destroyed, the button will also be deleted. It is no longer necessary to delete pButton to free the button's memory; its deletion has been delegated to the <code class="classname">MyWidget</code> object. </p> <p> <span class="application">gtkmm</span> also provides the <code class="methodname">set_manage()</code> method for all widgets. This can be used to generate the same result as <code class="function">manage()</code>, but is more tedious: </p> <p> foo.add( (w=new Gtk::Label("Hello"), w->set_manage(), &w) ); </p> <p> is the same as </p> <p> foo.add( manage(new Gtk::Label("Hello")) ); </p> <p> Of course, a top level container will not be added to another container. The programmer is responsible for destroying the top level container using one of the traditional C++ techniques. For instance, your top-level Window might just be an instance in your <code class="function">main()</code> function.. </p> </div> </div> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="sec-idle-functions.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"> </td> <td width="40%" align="right"> <a accesskey="n" href="sec-memory-shared-resources.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Idle Functions </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"> Shared resources</td> </tr> </table> </div> </body> </html>