Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Q1:</strong></p> <p>No. <code>@synthesize</code> does not modify the <code>-dealloc</code> for you. You have to <code>-release</code> the <code>name</code> yourself.</p> <p><strong>Q2:</strong></p> <p>Yes it will leak. Same reason as Q1.</p> <p><strong>Q3:</strong></p> <p>No it won't double-release. Same reason as Q1.</p> <p><strong>Q4:</strong></p> <p>Yes it will leak. Same reason as Q1.</p> <p><strong>Q5:</strong></p> <p>No it won't double-release. Same reason as Q1.</p> <hr> <p>You can check this yourself by overriding <code>-retain</code> and <code>-release</code> and <code>-dealloc</code> to report what is going on.</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface X : NSObject {} @end @implementation X -(oneway void)release { NSLog(@"Releasing %p, next count = %d", self, [self retainCount]-1); [super release]; } -(id)retain { NSLog(@"Retaining %p, next count = %d", self, [self retainCount]+1); return [super retain]; } -(void)dealloc { NSLog(@"Dealloc %p", self); [super dealloc]; } @end @interface Y : NSObject { X* x; } @property (nonatomic, retain) X* x; @end @implementation Y @synthesize x; - (void)dealloc { [x release]; [super dealloc]; } @end int main () { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; Y* y = [[Y alloc] init]; X* x = [[X alloc] init]; y.x = x; [y release]; [x release]; [pool drain]; return 0; } </code></pre> <p>In Q1, Q2 and Q4, the last <code>-retainCount</code> of <code>x</code> is 1, so there is a leak, and in Q3 and Q5 the last <code>-retainCount</code> is 0 and <code>-dealloc</code> is called, so there is no leak.</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