[Python-3000] Delayed reference counting idea

Ronald Oussoren ronaldoussoren at mac.com
Tue Sep 19 07:46:42 CEST 2006


On Sep 18, 2006, at 10:56 PM, Barry Warsaw wrote:

>
> I don't know, I hate macros. :)
>
> <talking from="my ass">
> It's been a long while since I programmed on the NeXT, so Mac folks
> here please chime in, but isn't there some Foundation idiom where
> temporary Objective-C objects didn't need to be explicitly released
> if their lifetime was exactly the duration of the function in which
> they were created?  ISTR something like the main event loop tracking
> such refcount=1 objects and deleting them automatically the next time
> through the loop.  Since Python has a main loop, I wonder if the same
> kind of trick couldn't be done here.

Objective-C, or rather Cocoa, uses reference counting but with a  
twist. Cocoa as autorelease pools (class NSAutoreleasePool) any  
object that is inserted in an autorelease pool gets its refcount  
decreased when the pool is deleted. Furthermore the main event loop  
creates a new pool at the start of the loop and removes it at the  
end, cleaning up all autoreleased objects.

Most cocoa methods return borrowed references (which they can do  
because of autorelease pools). When you know you won't hang onto an  
object until after the current iteration of the eventloop you can  
savely ignore reference counting. Only when you store a reference to  
an object somewhere (such as in an instance variable) you have to  
worry about the refcount.

<rant class="mini">
The annoying part of Cocoa's refcounting scheme is that they, unlike  
python, don't have a GC to clean up loops. This causes several parts  
of the Cocoa framework to ignore refcounts to avoid creating loops,  
which is rather annoying when you  write a bridge to Cocoa and want  
to hide reference counting details.
</rant>

Ronald

P.S. Apple is switching to a non-reference counting GC scheme in OSX  
10.5 (http://www.apple.com/macosx/leopard/xcode.html). 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2157 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20060919/230fec49/attachment.bin 


More information about the Python-3000 mailing list