Re: NSMutableDictionary autorelease chrashes application
Re: NSMutableDictionary autorelease chrashes application
- Subject: Re: NSMutableDictionary autorelease chrashes application
- From: Andy Lee <email@hidden>
- Date: Fri, 18 Jul 2008 13:48:10 -0400
On Jul 18, 2008, at 1:20 PM, Andy Lee 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.
And come to think of it, why mention that method X returns an object-I-
need-to-retain, but not mention it for method Y? If it's assumed I've
read the memory management rules, these should go without saying. So
maybe I am missing something.
--Andy
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."
That said, we do need to understand exactly what is meant when we
speak loosely this way.
--Andy
_______________________________________________
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
_______________________________________________
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