File: shared__ptr.html

package info (click to toggle)
telepathy-qt4 0.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,260 kB
  • ctags: 12,185
  • sloc: cpp: 29,241; xml: 23,692; ansic: 16,079; sh: 10,327; python: 3,080; makefile: 1,553
file content (52 lines) | stat: -rw-r--r-- 3,012 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
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>TelepathyQt4: Shared Pointer Usage</title>
  <link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="1">&nbsp;&nbsp;</td>
<td class="postheader" valign="center">
<a href="index.html">
<font color="#004faf">Home</font></a>&nbsp;&middot;
<a href="classes.html">
<font color="#004faf">All Classes</font></a>&nbsp;&middot;
<a href="namespaces.html">
<font color="#004faf">All Namespaces</font></a>&nbsp;&middot;
<a href="modules.html">
<font color="#004faf">Modules</font></a>&nbsp;&middot;
<a href="functions.html">
<font color="#004faf">Functions</font></a>&nbsp;&middot;
<a href="files.html">
<font color="#004faf">Files</font></a>
</td>
</tr>
</table>
</body>
</html>
<!-- Generated by Doxygen 1.6.3 -->
<div class="contents">


<h1><a class="anchor" id="shared_ptr">Shared Pointer Usage </a></h1><h2><a class="anchor" id="shared_ptr_overview">
Overview</a></h2>
<p>The Qt parent/child object model does not fit well with Telepathy-Qt4 object model, where in some places we either don't know the object parent or we can't use a parent, as the object can stay alive without it.</p>
<p>To avoid memory leaks, caused by objects that got instantiated and don't have any parent, we decided to make some of our objects reference counted, by making them inherit SharedData.</p>
<p>Making the object reference counted, does not guarantee that it will get deleted when nobody is referencing it.</p>
<p>When instantiating new classes that inherits SharedData the reference count is 0, this is referred to as the floating state. Again this may lead to memory leaks, caused by objects in the floating state that never get deleted.</p>
<p>So the solution is to put the object in a SharedPtr as soon as possible, letting the SharedPtr manage the object lifetime.</p>
<p>The pattern used is that classes inherit SharedData and are used together with SharedPtr. When the reference count hits 0, the object is deleted.</p>
<p>In order to assure that the object is put in a SharedPtr as soon as possible, our objects inheriting SharedData will have the constructor either private or protected, in case we want to support custom classes, and will have a public static create method that will return a SharedPtr pointing to the object instance.</p>
<p>Note that when developing custom classes, this pattern should be followed, to avoid objects in floating state, avoiding memory leaks. </p>
</div>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008-2010 Collabora Ltd. and Nokia Corporation</td>
<td width="30%" align="right"><div align="right">Telepathy-Qt4 0.3.6</div></td>
</tr></table></div></address>
</body>
</html>