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 1: The following pair is good if performance of "get" is important (say you expect it to be called it thousands of times a second):

- (NSString *) title {
return title;
}

- (void) setTitle: (NSString *)newTitle {
[title autorelease];
title = [newTitle copy]; // or retain, depending on object & usage
}

(as an optimization, you can do "if (title != newTitle)" in the set method).


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];
}

- (void) setTitle: (NSString *)newTitle {
if (title != newTitle) {
[title release];
title = [newTitle copy]; // or retain, depending on object & usage
}
}


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: 
 >Accessor methods and (auto)release <Memory trail> (From: Raphaël Delcroix <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.