Re: [Pyobjc-dev] Problem with threads
Brought to you by:
ronaldoussoren
From: Paul V. <pa...@vo...> - 2005-06-06 03:25:10
|
Hi bob, That worked perfectly for my polling thread (no more leaking :D) But then when I tried it at another point in my application. I get those same errors. This thread's sole purpose is to keep the gui responsive, as the task has to wait for server responses etc before it can complete. I create the pool just as you described and as I did with the other thread pool = NSAutoreleasePool.alloc().init() # thread work del pool '#thread work' makes both cocoa and python calls. I've looked over the document's you pointed to but from what they say it should work like you said. Any suggestions? Regards Paul Volpato On 06/06/2005, at 12:07 PM, Bob Ippolito wrote: > > On Jun 5, 2005, at 6:25 PM, Paul Volpato wrote: > > >> Hi, >> I'm having a problem with my application - I have a thread >> polling every few seconds for status changes and updating an >> NSTableView with each pass. - The part works fine. >> >> But if I add more 'cocoa' method calls to the polling loop I get >> an error like: >> >> _NSAutoreleaseNoPool(): Object 0x641fe0 of class NSCFDictionary >> autoreleased with no pool in place - just leaking >> >> This exact error happened when I tried to add >> 'tableViewSelectionDidChange_'. Which I have implemented as follows: >> >> def tableViewSelectionDidChange_(self, notification): >> row = self.instanceList.selectedRow() >> >> if row == -1: >> self.disableMenuItems(['Start', 'Stop', 'Restart', >> 'Export...', >> 'Delete']) >> else: >> status = self.IM.getStatus(row) >> if status == 'not running': >> self.disableMenuItems(['Stop', 'Restart', 'Export...', >> 'Import...']) >> else: >> self.disableMenuItems(['Start', 'Import...']) >> >> >> disableMenuItems() simply will disable an items that match the >> title of the list supplied. >> getStatus() returns a string 'running', 'not running'. >> >> The above implementation works fine - it just causes that error in >> the log (it acutally works) just messes up the log. >> > > Well, it sounds like you're breaking the rules here and using > threads where you shouldn't be. See <http://developer.apple.com/ > documentation/Cocoa/Conceptual/Multithreading/index.html> for > guidelines on thread safety, etc. > > In any case, that message means there is no NSAutoreleasePool in > the thread, and you're calling Objective-C code that wants one to > exist. The PyObjC intro <http://svn.red-bean.com/pyobjc/trunk/ > pyobjc/Doc/intro.html#working-with-threads> says this about threads: > Most of Cocoa, and thus PyObjC, requires an NSAutoreleasePool in > order to function properly. PyObjC does this automatically on the > first thread it is imported from, but other threads will require > explicit NSAutoreleasePool management. The following practice for > working with NSAutoreleasePool is recommended: > > pool = NSAutoreleasePool.alloc().init() > ... > del pool > Typically this will be done at the beginning and end of the thread. > It is important to use del before rebinding the pool local to > another NSAutoreleasePool instance, otherwise it will not have the > intended effect. > > For long running threads and tight loops, it can also be useful to > use this pattern in the body of the loop in order to optimize > memory usage. For example, NSRunLoop will be create a new > NSAutoreleasePool at the beginning of each run loop iteration and > release it at the end. > > -bob > > > > |