Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessor methods and (auto)release <Memory trail>



Method 2: Otherwise use the following pair, where the return value is autoreleased in the scope of the caller, which is more correct. This is also more (but not fully) thread-safe as the returned value is autoreleased in the calling thread.

- (NSString *) title {
return [[title retain] autorelease];
}

Sorry, perhaps I am just plain dumb, but in what situation this would bring any advantage to

-(NSString*)title { return title; }

? I just can't see it: we just increment the r.c. (presumably to two, since -- being in an instance variable -- it would be retained already), to be decremented back in some future... regardless in what thread this is done, what is it good for?

The API contract for most Cocoa APIs is that the following is valid:

{
str = [someObj title];
... do other stuff ...
... access str ...
}

If "do other stuff" ends up releasing the original recipient object (someObj), or more subtly, directly or indirectly changing the title of someObj (so that the previous one is released), str will be invalid unless it's lifetime was extended in the calling scope with retain/autorelease.

Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Accessor methods and (auto)release <Memory trail> (From: Ondra Cada <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.