16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

366 CHAPTER 17 ■ EXTENDING PYTHON<br />

Reference Counting<br />

If you haven’t worked with it before, reference counting will probably be one of the most foreign<br />

concepts you’ll encounter in this section, although it’s not really all that complicated. In <strong>Python</strong>,<br />

memory used is dealt with au<strong>to</strong>matically—you just create objects, and they disappear when<br />

you no longer use them. In C, this isn’t the case: you have <strong>to</strong> explicitly deallocate objects (or,<br />

rather, chunks of memory) that you’re no longer using. If you don’t, your program may start<br />

hogging more and more memory, and you have what’s called a memory leak.<br />

When writing <strong>Python</strong> extensions, you have access <strong>to</strong> the same <strong>to</strong>ols <strong>Python</strong> uses “under<br />

the hood” <strong>to</strong> manage memory, one of which is reference counting. The idea is that as long as<br />

some parts of your code have references <strong>to</strong> an object (that is, in C-speak, pointers pointing <strong>to</strong><br />

it), it should not be deallocated. However, once the number of references <strong>to</strong> an object hits zero,<br />

the number can no longer increase—there is no code that can create new references <strong>to</strong> it, and<br />

it’s just “free floating” in memory.<br />

At this point, it’s safe <strong>to</strong> deallocate it. Reference counting au<strong>to</strong>mates this process: You<br />

follow a set of rules where you increment or decrement the reference count for an object under<br />

various circumstances (through a part of the <strong>Python</strong> API), and if the count ever goes <strong>to</strong> zero,<br />

the object is au<strong>to</strong>matically deallocated. This means that no single piece of code has the sole<br />

responsibility for managing an object. You can create an object, return it from a function, and<br />

forget about it, safe in the knowledge that it will disappear when it is no longer needed.<br />

You use two macros called Py_INCREF and Py_DECREF <strong>to</strong> increment and decrement the<br />

reference count of an object, respectively. You can find detailed information about how <strong>to</strong> use<br />

these in the <strong>Python</strong> documentation (http://python.org/doc/ext/refcounts.html); here is the<br />

gist of it:<br />

• You can’t own an object, but you can own a reference <strong>to</strong> it. The reference count of an<br />

object is the number of owned references <strong>to</strong> it.<br />

• If you own a reference, you are responsible for calling Py_DECREF when you no longer<br />

need the reference.<br />

• If you borrow a reference temporarily, you should not call Py_DECREF when you’re<br />

finished with the object; that’s the responsibility of the owner. (You should certainly<br />

never use a borrowed reference after the owner has disposed of it. See the “Thin Ice”<br />

sections in the documentation for some more advice on staying safe.)<br />

• You can change a borrowed reference in<strong>to</strong> an owned reference by calling Py_INCREF.<br />

This creates a new owned reference; the original owner still owns the original reference.<br />

• When you receive an object as a parameter, it’s up <strong>to</strong> you whether you want the ownership<br />

of its reference transferred (for example, if you’re going <strong>to</strong> s<strong>to</strong>re it somewhere) or<br />

whether you simply want <strong>to</strong> borrow it. This should be documented clearly. If your function<br />

is called from <strong>Python</strong>, it’s safe <strong>to</strong> simply borrow—the object will live for the duration<br />

of the function call. If, however, your function is called from C, this cannot be guaranteed,<br />

and you might want <strong>to</strong> create an owned reference, and release it when you’re finished.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!