I have some questions about synthesized properties in Objective-C. The full list follows, but the basic question is this: How does the compiler ensure that the ivars for synthesized properties are properly released, even though my code may or may not include release methods in dealloc?
Note: I decided not to post these as individual questions because they are so closely related and because there are a handful of existing questions that touch on the individual issues without really getting to the heart of the matter.
Somewhat similar questions:
- Does property retain need a release?
- What's the difference between property and synthesize?
- Question on retain attribute with property and synthesize
Setup: Consider a class with a single property:
@interface Person : NSObject
{
NSString * name;
}
@property (nonatomic, retain) name;
@end
Question #1: The very basic case:
@implementation Person
@synthesize name;
@end
With this setup, I assume that name
will be automatically released whenever a Person
object is released. In my mind, the compiler simply inserts [name release]
into the dealloc
method as if I had typed it myself. Is that correct?
Question #2: If I choose to write my own dealloc
method for this class, and I omit a call to [name release]
, will that leak?
@implementation Person
@synthesize name;
- (void)dealloc { [super dealloc]; }
@end
Question #3: If I choose to write my own dealloc
method for this class, and I include a call to [name release]
, will that result in a double-release, since @synthesize
has already taken care of it for me?
@implementation Person
@synthesize name;
- (void)dealloc { [name release]; [super dealloc]; }
@end
Question #4: If I choose to write my own property accessor for this class, but I do not write my own dealloc
method, will name
be leaked?
@implementation Person
@dynamic name;
- (void)setName:(NSString *)newName
{
[newName retain];
[name release];
name = newName;
}
@end
Question #5: I have a feeling (based on experience) that none of the above scenarios will result in leaks or double-releases, since the language has been designed to avoid them. That, of course, raises the question of "how?". Is the compiler simply smart enough to keep track of every possible case? What if I were to do the following (note that this is a ludicrous example, just meant to illustrate my point):
void Cleanup(id object) { [object release]; }
@implementation Person
@synthesize name;
- (void)dealloc { Cleanup(name); }
@end
Would that fool the compiler into adding another [name release]
to the dealloc
method?