Re: [Pyobjc-dev] Headache over hacking an XML editor
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-01-13 21:46:27
|
On Monday, Jan 13, 2003, at 21:27 Europe/Amsterdam, Just van Rossum wrote: > I wrote: > >> I've attached a little Python Object browser using an NSOutlineView I >> recently hacked up. The attached is slightly newer than what I posted >> here before. > > 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. 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(). The call to release is necessary to adjust the reference count. The problem is that alloc is one of a few methods that returns a object that is owned by the caller, almost all other methods that return an object require that you call retain if you want to keep the object around. The bridge contains code to deal with this, but I didn't manage to convince Bill that automaticly adjusting the reference is a good idea. The mailinglist archive should contain more information. I still think that automaticly adjusting the references would be a good idea, and will get back to this later... Ronald (*) 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) |