File: sec-memory-shared-resources.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (114 lines) | stat: -rw-r--r-- 4,718 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Shared resources</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<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. 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="section">
<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&lt;&gt;</code> smartpointer. Cairomm has its own
smartpointer, <code class="classname">Cairo::RefPtr&lt;&gt;</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"><code class="code">auto pixbuf = Gdk::Pixbuf::create_from_file(filename);
</code></pre>

<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"><code class="code">auto width = 0;
if(pixbuf)
{
  width = pixbuf-&gt;get_width();
}
</code></pre>

<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"><code class="code">auto pixbuf2 = pixbuf;
</code></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" Fourth 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. Gtk::Builder</td>
</tr>
</table>
</div>
</body>
</html>