Re: Accessor methods and (auto)release <Memory trail>
Re: Accessor methods and (auto)release <Memory trail>
- Subject: Re: Accessor methods and (auto)release <Memory trail>
- From: Ali Ozer <email@hidden>
- Date: Tue, 30 Jul 2002 19:06:17 -0700
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.