[Openmcl-devel] cocoa bridge memory management changes?

Willem Rein Oudshoorn woudshoo at xs4all.nl
Fri Sep 24 13:02:45 PDT 2010


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
holds:

(#/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.  




More information about the Openmcl-devel mailing list