[Openmcl-devel] cocoa bridge memory management changes?
plkrueger at comcast.net
Fri Sep 24 15:29:05 CDT 2010
Yep, I think Willem hit it on the head. If you use a convenience constructor, then you don't own the object returned and so the memory leak is avoided. Of course you'd better make sure that you retain that object yourself if you need it beyond the end of the current autorelease pool.
I included the reference to convenience constructors that Willem was thinking about in my last (very long) email as well as in the tutorial in my contrib directory, but for those who don't want to wade through all that it is:
On Sep 24, 2010, at 3:02 PM, Willem Rein Oudshoorn wrote:
> Raffael Cavallaro <raffaelcavallaro at mac.com> writes:
>> On Sep 24, 2010, at 12:05 AM, Raffael Cavallaro wrote:
>>> Has the way memory management is done by the cocoa bridge changed
>>> significantly in the last week or so? I have some code that was
>>> working fine but now leaks quite a bit and I can't figure out why.
>> OK, this seems to be similar to a bug introduced then fixed a week or
>> so ago related to make-instance; if I change all of my object creation
>> to use:
>> (#/instanceWithSomething: ns:ns-some-class-or-other something)
> I have never heard of an objectiveC method named instanceWithSomething,
> but let me take a guess, if you talk about calls like:
> [NSColor colorWithDeviceRed: ...]
> or in lisp
> (#/colorWithDeviceRed:green:blue:alpha: ns:ns-color)
> than the answer is simple.
> In the foundation and appkit framework, due to historical reasons and
> maybe at one time an unfortunatel decision, the following conventions
> (#/init... (#/alloc ns:ns-...))
> (#/new... ns:ns-...) [ might be depracated in cocoa ]
> return objects which are retained. (which means they need 1 #/release
> message to go away)
> (#/colorWith.. ns:ns-color)
> (#/stringWith... ns:ns-string)
> return objects which are autoreleased. (so do not need a #/release
> message to go away.)
> [Note: I quickly looked up if I could find this documented somewhere,
> but I couldn't find it in the most obvious place.]
>> instead of:
>> (make-instance 'ns:ns-some-class-or-other :with-something something)
> This is equivalent, as far as I know, with
> the (#/initWith... (#/alloc ns:ns-some-class-or-other)) so fundamenally
> this is different from the (#/colorWith... ns:ns-color) case.
> fundamentallay different in the way that the (#/colorWith...)
> objective-C object will go away after a while (well, when the
> autorelease pool is emptied). But the other one, the (make-instance.. )
> one need an explicit #/release before it goes away.
> Wim Oudshoorn.
> P.S.: I hope this helps. I feel a bit stuck between the lisp language
> and the way I am used to talk about Objective C. I hope I didn't
> add too much confusion.
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
More information about the Openmcl-devel