Re: [Pyobjc-dev] Headache over hacking an XML editor
Brought to you by:
ronaldoussoren
From: Just v. R. <ju...@le...> - 2003-01-13 22:11:15
|
Ronald Oussoren wrote: > On Monday, Jan 13, 2003, at 21:27 Europe/Amsterdam, Just van Rossum > wrote: > > > Btw. there's in interesting thing going on in this app: if the > > PythonItem class is a subclass of NSObject everything works fine, > > but if it isn't (a pure Python class), the app crashes as soon as > > you click inside the tree widget. Maybe there's some refcount > > problem for Python objects passed to objc? > > This is a feature(*) of NSOutlineView. The outline view does not call > retain on the objects you return from methods like > outlineView:child:ofItem:, but it does keep around references to > those objects. This is documented in 'Using an Outline View Data > Source' in Apple's developer documents. Aha, I'll try to find that document. > The reason your code is crashing when PythonItem is not subclassed > from NSObject and runs correctly when it is twofold. > > First of all 'normal' python objects are brigded to Objective-C using > autoreleased proxy objects. Because the outline-view doesn't call > retain these will be released 'soon'. And because the outline-view > does actually use the pointer later your program crashes... > > If you subclass from NSObject your object is bridged as itself (more > or less). And if you keep around a reference to the objects you > return these objects won't go away before the outline-view uses the > pointer it stashed away. Which brings me to your second problem: You > code is leaking memory. > > PythonItem.__new__ should return cls.alloc().init().release(). Ha, when I make that change the app crashes right away ;-) But the reason is now more clear: I didn't keep any child references around at all, so the instances NSObject subclass would be alive just long enough (due to the autorelease pool magic I assume) and the non-NSObject subclass instances weren't. Or they indeed leaked. I changed the code to keep references to the children and everything works just fine as a non-NSObject subclass. But now it *does* crash when subclassing NSObject if I add .release() to the __new__ method, so something isn't right, most likely in my understanding of things... > (*) Actually I think this is not a feature but a bug. I've filed a > bug for this at Apple, but did not receive a response for this yet. I > hope Apple changes this, especially because adding the proper calls > to retain and release should be backward compatible (although I have > no idea what effect this would have on the performance of > NSOutlineView) It's at least a lousy feature :-( Just |