[Pyobjc-dev] Memory management bug using .new()
Brought to you by:
ronaldoussoren
From: Dirk S. <dir...@ma...> - 2009-06-18 19:14:57
|
Hi all, I don't think anyone ever uses this method – NSObject.new() – anymore, but in case someone does: I just ran into a reproducable problem with it. When you create a new cocoa object within an instance method of a Cocoa class implemented in Python, normally that fresh object is released when the scope dissapears. (that is, when you're not storing a reference to it in an attribute that exists outside of the method's scope). e.g.: class MyAmazingViewController(NSViewController): def animate(self): animation = NSViewAnimation.alloc().init() # insert code to create an array/list of NSDictionaries describing the animations animation.setViewAnimations_(theAboveMentionedHypotheticalArray) animation.setDuration_(5.0) animation.setAnimationBlockingMode_(NSAnimationNonblocking) animation.startAnimation() ( if the spaces got stripped from my example somehow, snippet also pasted here: http://pastie.org/516369 ) After control returns from the "animate" method, and after the animation is done, it is automatically released. However if you replace "NSViewAnimation.alloc().init()", with "NSViewAnimation.new()", the animation is not automatically released. Consequentially, any views or windows included in the dictionaries that form the viewAnimations array are also retained, causing quite a significant leak. Juding from the NSObject class docs, calling "new()" on a class should do exactly the same thing as "alloc().init()", but PyObjC doesn't seem to treat it the same way. http://tinyurl.com/nsobject-new Can anyone enlighten me whether this is a bug, a feature, or simply something I'm misunderstanding? Thanks! - Dirk |