Re: [Pyobjc-dev] Re: [Pyobjc-checkins] CVS: pyobjc/Modules/objc objc_util.h,1.24,1.25 objc_util.m,1.
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-04-07 18:35:09
|
On Apr 7, 2004, at 1:43 PM, Ronald Oussoren wrote: > > On 7-apr-04, at 19:30, Bob Ippolito wrote: > >> >> On Apr 7, 2004, at 1:17 PM, Ronald Oussoren wrote: >> >>> >>> On 7-apr-04, at 19:11, Bob Ippolito wrote: >>>> >>>> Because creating one is harmless, and if you don't create one you >>>> leak memory. >>> >>> Creating one is not harmless if you never release it, you still leak >>> memory without any message about this. >>> >>> We currently leak 1 NSInvocation while creating the >>> NSAutoreleasePool in a new thread, and this is logged. Because it is >>> logged I get nagged anytime I run the unittests, which makes it more >>> likely that this problem will be fixed. >> >> The NSAutoreleasePool goes away whenever the threadstate dict does, >> and that's up to Python to decide. My best guess is that it goes >> away as soon as you call PyGILState_Release, but I have not yet >> looked at the implementation. > > It goes away when the gilstate_counter is 0, which is when you call > PyGILState_Release. > > I'm not sure what happens when you call into ObjC from a thread that > already has a python threadstate (e.g. a thread created using > thread/threading). From a fairly light reading of the source I assume > that the pool won't be released until the thread dies. If ObjC calls into a thread that already has a Python threadstate it will not create an NSAutoreleasePool, if you look closely at the code you can see that it will only execute that code if PyGILState_Ensure is creating a new threadstate. As far as this code is concerned, there are 3 kinds of threads: 1) The main thread. It already has a global NSAutoreleasePool (by PyObjC initialization) and a Python threadstate. When it gets a callback from ObjC, since it has a threadstate already, it just does PyGILState_Ensure(). 2) A Python-created thread. You must create an NSAutoreleasePool yourself if you create a python thread and then start calling into ObjC from it. Since it has a threadstate already, it just does PyGILState_Ensure(). 3) An ObjC-created thread. These do not have a threadstate already, so it will do PyGILState_Ensure() and create a NSAutoreleasePool which will be collected at PyGILState_Release() time. -bob |