Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ext / refcountsInPython.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="ext.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='last' href='about.html' title='About this document...' />
10<link rel='help' href='about.html' title='About this document...' />
11<link rel="next" href="ownershipRules.html" />
12<link rel="prev" href="refcounts.html" />
13<link rel="parent" href="refcounts.html" />
14<link rel="next" href="ownershipRules.html" />
15<meta name='aesop' content='information' />
16<title>1.10.1 Reference Counting in Python
17</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="1.10 Reference Counts"
25 href="refcounts.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="1.10 Reference Counts"
28 href="refcounts.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="1.10.2 Ownership Rules"
31 href="ownershipRules.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><img src='../icons/blank.png'
38 border='0' height='32' alt='' width='32' /></td>
39<td class='online-navigation'><img src='../icons/blank.png'
40 border='0' height='32' alt='' width='32' /></td>
41</tr></table>
42<div class='online-navigation'>
43<b class="navlabel">Previous:</b>
44<a class="sectref" rel="prev" href="refcounts.html">1.10 Reference Counts</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="refcounts.html">1.10 Reference Counts</A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="ownershipRules.html">1.10.2 Ownership Rules</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H2><A NAME="SECTION0031010000000000000000"></A><A NAME="refcountsInPython"></A>
55<BR>
561.10.1 Reference Counting in Python
57
58</H2>
59
60<P>
61There are two macros, <code>Py_INCREF(x)</code> and <code>Py_DECREF(x)</code>,
62which handle the incrementing and decrementing of the reference count.
63<tt class="cfunction">Py_DECREF()</tt> also frees the object when the count reaches zero.
64For flexibility, it doesn't call <tt class="cfunction">free()</tt> directly -- rather, it
65makes a call through a function pointer in the object's <i class="dfn">type
66object</i>. For this purpose (and others), every object also contains a
67pointer to its type object.
68
69<P>
70The big question now remains: when to use <code>Py_INCREF(x)</code> and
71<code>Py_DECREF(x)</code>? Let's first introduce some terms. Nobody
72``owns'' an object; however, you can <i class="dfn">own a reference</i> to an
73object. An object's reference count is now defined as the number of
74owned references to it. The owner of a reference is responsible for
75calling <tt class="cfunction">Py_DECREF()</tt> when the reference is no longer
76needed. Ownership of a reference can be transferred. There are three
77ways to dispose of an owned reference: pass it on, store it, or call
78<tt class="cfunction">Py_DECREF()</tt>. Forgetting to dispose of an owned reference
79creates a memory leak.
80
81<P>
82It is also possible to <i class="dfn">borrow</i><A NAME="tex2html2"
83 HREF="#foot403"><SUP>1.2</SUP></A> a reference to an object. The borrower
84of a reference should not call <tt class="cfunction">Py_DECREF()</tt>. The borrower must
85not hold on to the object longer than the owner from which it was
86borrowed. Using a borrowed reference after the owner has disposed of
87it risks using freed memory and should be avoided
88completely.<A NAME="tex2html3"
89 HREF="#foot535"><SUP>1.3</SUP></A>
90<P>
91The advantage of borrowing over owning a reference is that you don't
92need to take care of disposing of the reference on all possible paths
93through the code -- in other words, with a borrowed reference you
94don't run the risk of leaking when a premature exit is taken. The
95disadvantage of borrowing over leaking is that there are some subtle
96situations where in seemingly correct code a borrowed reference can be
97used after the owner from which it was borrowed has in fact disposed
98of it.
99
100<P>
101A borrowed reference can be changed into an owned reference by calling
102<tt class="cfunction">Py_INCREF()</tt>. This does not affect the status of the owner from
103which the reference was borrowed -- it creates a new owned reference,
104and gives full owner responsibilities (the new owner must
105dispose of the reference properly, as well as the previous owner).
106
107<P>
108<BR><HR><H4>Footnotes</H4>
109<DL>
110<DT><A NAME="foot403">...borrow</A><A
111 href="refcountsInPython.html#tex2html2"><SUP>1.2</SUP></A></DT>
112<DD>The metaphor of
113``borrowing'' a reference is not completely correct: the owner still
114has a copy of the reference.
115
116</DD>
117<DT><A NAME="foot535">...
118completely.</A><A
119 href="refcountsInPython.html#tex2html3"><SUP>1.3</SUP></A></DT>
120<DD>Checking that the reference count is at least 1
121<strong>does not work</strong> -- the reference count itself could be in
122freed memory and may thus be reused for another object!
123
124</DD>
125</DL>
126<DIV CLASS="navigation">
127<div class='online-navigation'>
128<p></p><hr />
129<table align="center" width="100%" cellpadding="0" cellspacing="2">
130<tr>
131<td class='online-navigation'><a rel="prev" title="1.10 Reference Counts"
132 href="refcounts.html"><img src='../icons/previous.png'
133 border='0' height='32' alt='Previous Page' width='32' /></A></td>
134<td class='online-navigation'><a rel="parent" title="1.10 Reference Counts"
135 href="refcounts.html"><img src='../icons/up.png'
136 border='0' height='32' alt='Up One Level' width='32' /></A></td>
137<td class='online-navigation'><a rel="next" title="1.10.2 Ownership Rules"
138 href="ownershipRules.html"><img src='../icons/next.png'
139 border='0' height='32' alt='Next Page' width='32' /></A></td>
140<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
141<td class='online-navigation'><a rel="contents" title="Table of Contents"
142 href="contents.html"><img src='../icons/contents.png'
143 border='0' height='32' alt='Contents' width='32' /></A></td>
144<td class='online-navigation'><img src='../icons/blank.png'
145 border='0' height='32' alt='' width='32' /></td>
146<td class='online-navigation'><img src='../icons/blank.png'
147 border='0' height='32' alt='' width='32' /></td>
148</tr></table>
149<div class='online-navigation'>
150<b class="navlabel">Previous:</b>
151<a class="sectref" rel="prev" href="refcounts.html">1.10 Reference Counts</A>
152<b class="navlabel">Up:</b>
153<a class="sectref" rel="parent" href="refcounts.html">1.10 Reference Counts</A>
154<b class="navlabel">Next:</b>
155<a class="sectref" rel="next" href="ownershipRules.html">1.10.2 Ownership Rules</A>
156</div>
157</div>
158<hr />
159<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
160</DIV>
161<!--End of Navigation Panel-->
162<ADDRESS>
163See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
164</ADDRESS>
165</BODY>
166</HTML>