I’ve been reading an apple document on Memory Management and am now a bit confused regarding the recommended implementation of Accessors. Apple cites 3 approaches in implementing accessors.
Technique 1
I’ve copied the code from the first technique which reflects: “Getter retains and autoreleases the value before returning it; setter releases the old value and retains (or copies) the new value.” This first technique is stated to be more robust but suffers performance penalties on frequently called getters.
- (NSString*) title {
return [[title retain] autorelease];
}
- (void) setTitle: (NSString*) newTitle {
if (title != newTitle) {
[title release];
title = [newTitle retain]; // Or copy, depending on your needs.
}
}
skipping Technique 2
Technique 3
The third technique is better for frequently called setters and getters. This is also the method I’ve always followed.
- (NSString*) title {
return title;
}
- (void) setTitle: (NSString*) newTitle {
if (newTitle != title) {
[title release];
title = [newTitle retain]; // Or copy, depending on your needs.
}
}
My questions are:
-
(Technique 1) The setter first releases the existing value even if it doesn’t point to anything. This would send a message to nil which I understand is supported in Objective-C but still looks odd. Am I understanding this correctly?
-
(Technique 1) Why is the retain stacked inside an autorelease?
-
(Technique 1) Is the caller whom uses the getter expected to call release after they’re done with the object?
The apple developer documentation page can be found at: Memory Management Programming Guide – Accessor Methods
Yup. Messages to
nilmatter not.That guarantees that the following doesn’t break:
No.