File: ch18.html

package info (click to toggle)
gtkmm2.0 2.2.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,872 kB
  • ctags: 51,600
  • sloc: xml: 73,173; cpp: 20,565; sh: 8,608; perl: 2,702; makefile: 1,233
file content (100 lines) | stat: -rw-r--r-- 7,349 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter18.Memory management</title><meta name="generator" content="DocBook XSL Stylesheets V1.64.1"><link rel="home" href="index.html" title="Programming with gtkmm2"><link rel="up" href="index.html" title="Programming with gtkmm2"><link rel="previous" href="ch17s03.html" title="Idle Functions"><link rel="next" href="ch18s02.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">Chapter18.Memory management</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch17s03.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="ch18s02.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="sec-Memory"></a>Chapter18.Memory management</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch18.html#id2518714">Widgets</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch18.html#id2518720">Normal C++ memory management</a></span></dt><dt><span class="sect2"><a href="ch18.html#id2518858">Managed Widgets</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch18s02.html">Shared resources</a></span></dt></dl></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2518714"></a>Widgets</h2></div></div><div></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2518720"></a>Normal C++ memory management</h3></div></div><div></div></div><p>
gtkmm 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 <tt class="literal">new</tt> and
<tt class="literal">delete</tt> 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" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2518755"></a>Class Scope widgets</h4></div></div><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 <tt class="literal">delete</tt> 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 &lt;gtkmm/button.h&gt;
class Foo
{
private:
  Gtk::Button theButton;
  // will be destroyed when the Foo object is destroyed
};
</pre><p>
</p></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2518798"></a>Function scope widgets</h4></div></div><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" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2518819"></a>Dynamic allocation with new and delete</h4></div></div><div></div></div><p> 
Although, in most cases, the programmer will prefer to allow containers to
automatically destroy their children using manage() (see below), the programmer is not
required to use manage(). The traditional <tt class="literal">new</tt>
and <tt class="literal">delete</tt> 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" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2518858"></a>Managed Widgets</h3></div></div><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 <tt class="literal">manage()</tt> and
pack it into its container with <tt class="literal">add()</tt>.  Now, the
widget will be destroyed whenever its container is destroyed.
</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2518885"></a>Dynamic allocation with manage() and add()</h4></div></div><div></div></div><p>
gtkmm provides the manage() and add() 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 manage() 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 MyWidget is 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 MyWidget.
</p><p>
gtkmm also provides the set_manage() method for all widgets.
This can be used to generate the same result as manage(), but
is more tedious: 
</p><p>
foo.add( (w=new Gtk::Label("Hello"), w-&gt;set_manage(), &amp;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 main() 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="ch17s03.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch18s02.html">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">Home</a></td><td width="40%" align="right" valign="top">Shared resources</td></tr></table></div></body></html>