Note that there are some explanatory texts on larger screens.

plurals
  1. POHow is release handled for @synthesized retain properties?
    text
    copied!<p>I have some questions about synthesized properties in Objective-C. The full list follows, but the basic question is this: <strong>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?</strong></p> <p>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 <em>touch</em> on the individual issues without really getting to the heart of the matter.</p> <p>Somewhat similar questions:</p> <ul> <li><a href="https://stackoverflow.com/questions/1782893/does-propertyretain-need-a-release">Does property retain need a release?</a></li> <li><a href="https://stackoverflow.com/questions/806379/whats-the-difference-between-property-and-synthesize">What's the difference between property and synthesize?</a></li> <li><a href="https://stackoverflow.com/questions/1980488/question-on-retain-attribute-with-property-and-synthesize">Question on retain attribute with property and synthesize</a></li> </ul> <hr> <p><strong>Setup:</strong> Consider a class with a single property:</p> <pre><code>@interface Person : NSObject { NSString * name; } @property (nonatomic, retain) name; @end </code></pre> <hr> <p><strong>Question #1:</strong> The very basic case:</p> <pre><code>@implementation Person @synthesize name; @end </code></pre> <p>With this setup, I assume that <code>name</code> will be automatically released whenever a <code>Person</code> object is released. In my mind, the compiler simply inserts <code>[name release]</code> into the <code>dealloc</code> method as if I had typed it myself. Is that correct?</p> <hr> <p><strong>Question #2:</strong> If I choose to write my own <code>dealloc</code> method for this class, and I omit a call to <code>[name release]</code>, will that leak?</p> <pre><code>@implementation Person @synthesize name; - (void)dealloc { [super dealloc]; } @end </code></pre> <hr> <p><strong>Question #3:</strong> If I choose to write my own <code>dealloc</code> method for this class, and I <em>include</em> a call to <code>[name release]</code>, will that result in a double-release, since <code>@synthesize</code> has already taken care of it for me?</p> <pre><code>@implementation Person @synthesize name; - (void)dealloc { [name release]; [super dealloc]; } @end </code></pre> <hr> <p><strong>Question #4:</strong> If I choose to write my own property accessor for this class, but I <em>do not</em> write my own <code>dealloc</code> method, will <code>name</code> be leaked? </p> <pre><code>@implementation Person @dynamic name; - (void)setName:(NSString *)newName { [newName retain]; [name release]; name = newName; } @end </code></pre> <hr> <p><strong>Question #5:</strong> I have a feeling (based on experience) that <em>none</em> 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):</p> <pre><code>void Cleanup(id object) { [object release]; } @implementation Person @synthesize name; - (void)dealloc { Cleanup(name); } @end </code></pre> <p>Would that fool the compiler into adding another <code>[name release]</code> to the <code>dealloc</code> method?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload