In my setter I have a incoming string that I am copying to avoid any trouble should the original be modified.
- (void)setName:(NSString *)newName{
if(name != newName) {
[name release];
name = [newName copy];
}
}
My question is: as I am doing a copy where should I release it, or do I just do an autorelease? i.e.
- (void)setName:(NSString *)newName{
if(name != newName) {
[name release];
name = [[newName copy] autorelease];
}
}
gary
As I said on your other question,
autoreleasetranslates to “send yourselfreleaselater”. That means that it counts as a release.So, your second example releases the old value of
name(good), then makes a copy of the new one (good), then puts the new one in line to be released (bad), then puts this now-doomed object into the instance variable (really bad).The first example is correct, because you still hold a retention on the object after you put it in the instance variable.
As I also said on your other question, I think you should review the memory-management rules.