Re: Memory Management
Re: Memory Management
- Subject: Re: Memory Management
- From: Wade Tregaskis <email@hidden>
- Date: Wed, 30 Jul 2003 11:48:31 +1000
- (void)setName:(NSString *)newNamemet {
[newName retain];
[_name release];
_name = newName;
}
This is fine.
Thread safe ?
No, obviously not. It would need a lock around it, if you were to do
it that way. The question then becomes: what's faster, a lock or an
autorelease? In most situations an autorelease, I would think, since
it would be a once-off, versus potentially having to acquire the lock
many times. Unless of course your value changes far more frequently
than you access it, in which case I suppose a lock might be faster.
You would always need a lock around it. Autorelease is not magic. It
certainly won't get you thread safety.
Yes it will, in this case. The problem with the previous example
(above) is that you release [and possibly dealloc] your existing
instance, then could have some other code which refers to _name before
you change it to newName. _name at this point is a dangling pointer.
If instead you autoreleased your existing instance, any chance
reference to _name before you change it would still return a valid,
existing object.
Wade Tregaskis
-- Sed quis custodiet ipsos custodes?
_______________________________________________
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.