Re: [Pyobjc-dev] Reference Counting
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2004-04-08 16:59:34
|
On 8-apr-04, at 17:54, Michael Tsai wrote: > Hi, > > I've been happily using PyObjC to write little scripts for testing > and automation. Now I'd like to use it to write some apps, and so I > want to make sure that I don't leak memory and also have a > reasonable memory high-water mark. The documentation says: > > The `Cocoa libraries`_, and most (if not all) other class > libraries for Objective-C use explicit reference counting to > manage memory. The methods ``retain``, ``release`` and > ``autorelease`` are used to manage these reference counts. You > won't have to manage reference counts in Python, the bridge does > all that work for you. > > Wow, that's nice. However, I'm always a bit skeptical of unexplained > magic, and indeed it doesn't appear to be *quite* that simple (even > ignoring weakrefs). The code: > > while 1: > NSArray.alloc().init() > > does indeed run with a constant amount of memory. However: > > while 1: > NSArray.array() > will use memory without bound unless I put in an autorelease pool: That's correct, the bridge does not (and can not) hide the fact that an autorelease pool exists. This is not a problem in the normal use-case for PyObjC: writing GUI programs where you wouldn't use such a pool in Cocoa either. [... some interesting examples where PyObjC seems to be leaking memory snipped ... ] That's very interesting. I'm going to look into this, it's definitely my intention to make sure that the bridge won't leak memory. BTW. What version of PyObjC are you using? > One more question. It seems that PyObjC uses naming conventions to > determine whether a selector transfers ownership. Does this mean > that bad things will happen if I have an Objective-C class with an > accessor method called -initialValue? That is, will PyObjC fail to > retain the returned value because it thinks this is an -init method? The method that tranfers ownership is named 'alloc', or 'copy'. The initializer doesn't come into play. Your example is a method name that is incorrectly recognized as an initializer, which does have a minor influence on the behaviour of the bridge, although not one that you should normally run into (the bridge knows that +alloc returns uninitialized objects and won't -release such objects unless an initializer method has been called). Ronald -- X|support bv http://www.xsupport.nl/ T: +31 610271479 F: +31 204416173 |