Re: NSMutableDictionary autorelease chrashes application
Re: NSMutableDictionary autorelease chrashes application
- Subject: Re: NSMutableDictionary autorelease chrashes application
- From: "Shawn Erickson" <email@hidden>
- Date: Fri, 18 Jul 2008 11:27:42 -0700
On Fri, Jul 18, 2008 at 10:20 AM, Andy Lee <email@hidden> wrote:
> On Jul 18, 2008, at 10:49 AM, Shawn Erickson wrote:
>>
>> I should more clearly note that objectForKey: is not returning an
>> "autoreleased" object. Also even if it did it would be an
>> implementation detail (unless documented in the API docs).
>>
>> It is returning a reference to an object that the vColors dictionary
>> has a retained reference to (Cocoa collections retain what you add to
>> them). At this point in time you have know idea of the lifetime of the
>> object you get back (it could go away when the vColors dictionary goes
>> away or it could live longer). If you need something to stay around
>> outside of the method you are in (assuming no side effects down stream
>> in that method) then you must retain that object for a long as you
>> need it.
>
> When the docs say a method returns an "autoreleased" object, I assume it
> means what you say above about objectForKey:. Otherwise, why mention it at
> all? Taken literally, the fact that it is autoreleased tells us nothing
> about its life expectancy, and unless I'm missing something I can't imagine
> why else I'd care to know.
>
> An Xcode search for "autoreleased" turns up a few cases. For example, the
> doc for -[IMKInputController delegate] says "The returned object is an
> autoreleased object." So what? We have no idea how many times the delegate
> was retained before being assigned to our IMKInputController instance.
>
> Unless Apple defines another adjective for this purpose, it seems to me that
> "autoreleased" is a reasonable shorthand for "you must retain it if you want
> it to stick around, or you *may* have a dangling pointer." Similarly,
> "retained" is a reasonable shorthand for "you must balance the method you
> just called with a release, or you'll have a memory leak."
Autorelease has a specific meaning (not the one you imply above) and
as you note few APIs document it since it really is unimportant to
know. My point was it is an implementation detail.
Apple attempts to consistently talk in terms of an object ownership.
Did you create an object or retain it, if so then you own it... if not
then you don't. If you own it then it will persist at least until you
release ownership. etc. The only issue I have with the term ownership
is that to most folks "owning" something implies a single owner but in
the case of objects you can have multiple owners at any given time. As
long as folks understand that then ownership works well to describe
this.
-Shawn
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden