[Pythonmac-SIG] lots of Python-Cocoa errors in XCode console window...

Jack Jansen Jack.Jansen at cwi.nl
Mon Nov 3 13:32:26 CET 2008


On 3 nov 2008, at 00:02, Bill Janssen wrote:

> has <hengist.podd at virgin.net> wrote:
>
>> You're leaking memory, which isn't a good thing.
>>
>> Are you using background threads? PyObjC automatically creates an
>> autorelease pool for the main thread only; you need to provide
>> autorelease pools for other threads yourself.
>
> Yes, I am using background threads.  I have some functions triggered  
> by
> actions which make calls on remote services, and so may take  
> arbitrarily
> long to execute; I fork worker threads to do that fetch.  I'm using
> Python's threading.Thread; should my "target" function look like this?
>
>   def thread_target(*args):
>      pool = NSAutoreleasePool.alloc().init()
>      try:
>          ...
>      finally:
>          del pool

I don't even think you need the try/finally and the del:
def thread_target(*args):
	pool = NSAutoReleasePool.alloc().init()
	...

The pool will be discarded when the thread exits, and that's that.

Of course, if you want the pool to be emptied more often you have to  
add code to do that.


--
Jack Jansen, <Jack.Jansen at cwi.nl>, http://www.cwi.nl/~jack
If I can't dance I don't want to be part of your revolution -- Emma  
Goldman




More information about the Pythonmac-SIG mailing list