1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Object memory management: GObject Reference Manual</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="chapter-gobject.html" title="The GObject base class">
9 <link rel="prev" href="chapter-gobject.html" title="The GObject base class">
10 <link rel="next" href="gobject-properties.html" title="Object properties">
11 <meta name="generator" content="GTK-Doc V1.25.1 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
16 <td width="100%" align="left" class="shortcuts"></td>
17 <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
18 <td><a accesskey="u" href="chapter-gobject.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
19 <td><a accesskey="p" href="chapter-gobject.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
20 <td><a accesskey="n" href="gobject-properties.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="gobject-memory"></a>Object memory management</h2></div></div></div>
26 The memory-management API for GObjects is a bit complicated but the idea behind it
27 is pretty simple: the goal is to provide a flexible model based on reference counting
28 which can be integrated in applications which use or require different memory management
29 models (such as garbage collection). The methods which are used to
30 manipulate this reference count are described below.
33 <div class="titlepage"><div><div><h3 class="title">
34 <a name="gobject-memory-refcount"></a>Reference count</h3></div></div></div>
36 The functions <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-ref" title="g_object_ref ()">g_object_ref</a></code>/<code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-unref" title="g_object_unref ()">g_object_unref</a></code> respectively
37 increase and decrease the reference count. These functions are
39 <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-clear-object" title="g_clear_object ()">g_clear_object</a></code>
40 is a convenience wrapper around <code class="function">g_object_unref</code>
41 which also clears the pointer passed to it.
44 The reference count is initialized to one by
45 <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-new" title="g_object_new ()">g_object_new</a></code> which means that the caller
46 is currently the sole owner of the newly-created reference.
47 When the reference count reaches zero, that is,
48 when <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-unref" title="g_object_unref ()">g_object_unref</a></code> is called by the last client holding
49 a reference to the object, the <span class="emphasis"><em>dispose</em></span> and the
50 <span class="emphasis"><em>finalize</em></span> class methods are invoked.
53 Finally, after <span class="emphasis"><em>finalize</em></span> is invoked,
54 <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-free-instance" title="g_type_free_instance ()">g_type_free_instance</a></code> is called to free the object instance.
55 Depending on the memory allocation policy decided when the type was registered (through
56 one of the <code class="function">g_type_register_*</code> functions), the object's instance
57 memory will be freed or returned to the object pool for this type.
58 Once the object has been freed, if it was the last instance of the type, the type's class
59 will be destroyed as described in <a class="xref" href="gtype-instantiable-classed.html" title="Instantiable classed types: objects">the section called “Instantiable classed types: objects”</a> and
60 <a class="xref" href="gtype-non-instantiable-classed.html" title="Non-instantiable classed types: interfaces">the section called “Non-instantiable classed types: interfaces”</a>.
63 The table below summarizes the destruction process of a GObject:
66 <a name="gobject-destruction-table"></a><p class="title"><b>Table 5. <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-unref" title="g_object_unref ()">g_object_unref</a></code></b></p>
67 <div class="table-contents"><table class="table" summary="g_object_unref" border="1">
74 <th align="left">Invocation time</th>
75 <th align="left">Function invoked</th>
76 <th align="left">Function's parameters</th>
81 <td rowspan="2" align="left">Last call to <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-unref" title="g_object_unref ()">g_object_unref</a></code> for an instance
84 <td align="left">target type's dispose class function</td>
85 <td align="left">GObject instance</td>
87 When dispose ends, the object should not hold any reference to any other
88 member object. The object is also expected to be able to answer client
89 method invocations (with possibly an error code but no memory violation)
90 until finalize is executed. dispose can be executed more than once.
91 dispose should chain up to its parent implementation just before returning
96 <td align="left">target type's finalize class function</td>
97 <td align="left">GObject instance</td>
99 Finalize is expected to complete the destruction process initiated by
100 dispose. It should complete the object's destruction. finalize will be
102 finalize should chain up to its parent implementation just before returning
104 The reason why the destruction process is split is two different phases is
105 explained in <a class="xref" href="gobject-memory.html#gobject-memory-cycles" title="Reference counts and cycles">the section called “Reference counts and cycles”</a>.
109 <td rowspan="4" align="left">Last call to <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-unref" title="g_object_unref ()">g_object_unref</a></code> for the last
110 instance of target type
112 <td align="left">interface's <code class="function">interface_finalize</code> function</td>
113 <td align="left">On interface's vtable</td>
114 <td>Never used in practice. Unlikely you will need it.</td>
117 <td align="left">interface's <code class="function">base_finalize</code> function</td>
118 <td align="left">On interface's vtable</td>
119 <td>Never used in practice. Unlikely you will need it.</td>
122 <td align="left">target type's <code class="function">class_finalize</code> function</td>
123 <td align="left">On target type's class structure</td>
124 <td>Never used in practice. Unlikely you will need it.</td>
127 <td align="left">type's <code class="function">base_finalize</code> function</td>
128 <td align="left">On the inheritance tree of classes from fundamental type to target type.
129 <code class="function">base_init</code> is invoked once for each class structure.</td>
130 <td>Never used in practice. Unlikely you will need it.</td>
135 <p><br class="table-break">
139 <div class="titlepage"><div><div><h3 class="title">
140 <a name="gobject-memory-weakref"></a>Weak References</h3></div></div></div>
142 Weak references are used to monitor object finalization:
143 <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-weak-ref" title="g_object_weak_ref ()">g_object_weak_ref</a></code> adds a monitoring callback which does
144 not hold a reference to the object but which is invoked when the object runs
145 its dispose method. As such, each weak ref can be invoked more than once upon
146 object finalization (since dispose can run more than once during object
150 <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-weak-unref" title="g_object_weak_unref ()">g_object_weak_unref</a></code> can be used to remove a monitoring
151 callback from the object.
154 Weak references are also used to implement <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-add-weak-pointer" title="g_object_add_weak_pointer ()">g_object_add_weak_pointer</a></code>
155 and <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-remove-weak-pointer" title="g_object_remove_weak_pointer ()">g_object_remove_weak_pointer</a></code>. These functions add a weak reference
156 to the object they are applied to which makes sure to nullify the pointer given by the user
157 when object is finalized.
160 Similarly, <a class="link" href="gobject-The-Base-Object-Type.html#GWeakRef" title="GWeakRef"><span class="type">GWeakRef</span></a> can be
161 used to implement weak references if thread safety is required.
165 <div class="titlepage"><div><div><h3 class="title">
166 <a name="gobject-memory-cycles"></a>Reference counts and cycles</h3></div></div></div>
168 GObject's memory management model was designed to be easily integrated in existing code
169 using garbage collection. This is why the destruction process is split in two phases:
170 the first phase, executed in the dispose handler is supposed to release all references
171 to other member objects. The second phase, executed by the finalize handler is supposed
172 to complete the object's destruction process. Object methods should be able to run
173 without program error in-between the two phases.
176 This two-step destruction process is very useful to break reference counting cycles.
177 While the detection of the cycles is up to the external code, once the cycles have been
178 detected, the external code can invoke <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-run-dispose" title="g_object_run_dispose ()">g_object_run_dispose</a></code> which
179 will indeed break any existing cycles since it will run the dispose handler associated
180 to the object and thus release all references to other objects.
183 This explains one of the rules about the dispose handler stated earlier:
184 the dispose handler can be invoked multiple times. Let's say we
185 have a reference count cycle: object A references B which itself references object A.
186 Let's say we have detected the cycle and we want to destroy the two objects. One way to
187 do this would be to invoke <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-run-dispose" title="g_object_run_dispose ()">g_object_run_dispose</a></code> on one of the
191 If object A releases all its references to all objects, this means it releases its
192 reference to object B. If object B was not owned by anyone else, this is its last
193 reference count which means this last unref runs B's dispose handler which, in turn,
194 releases B's reference on object A. If this is A's last reference count, this last
195 unref runs A's dispose handler which is running for the second time before
196 A's finalize handler is invoked !
199 The above example, which might seem a bit contrived, can really happen if
200 GObjects are being handled by language bindings — hence the rules for
201 object destruction should be closely followed.
206 <hr>Generated by GTK-Doc V1.25.1</div>