<p>As I understand it, anything created with an <strong>alloc</strong>, <strong>new</strong>, or <strong>copy</strong> needs to be manually released. For example:</p>

<pre><code>int main(void) {
    NSString *string;
    string = [[NSString alloc] init];
    /* use the string */
    [string release];
}
</code></pre>

<p>My question, though, is wouldn't this be just as valid?:</p>

<pre><code>int main(void) {
    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init];
    NSString *string;
    string = [[[NSString alloc] init] autorelease];
    /* use the string */
    [pool drain];
}
</code></pre>

[{'answer_id': 65483, 'author': 'kperryua', 'author_id': 72126, 'author_profile': 'https://Stackoverflow.com/users/72126', 'pm_score': 7, 'selected': True, 'text': '<p>Yes, your second code snippit is perfectly valid.</p>\n\n<p>Every time -autorelease is sent to an object, it is added to the inner-most autorelease pool. When the pool is drained, it simply sends -release to all the objects in the pool.</p>\n\n<p>Autorelease pools are simply a convenience that allows you to defer sending -release until "later". That "later" can happen in several places, but the most common in Cocoa GUI apps is at the end of the current run loop cycle.</p>\n'}, {'answer_id': 65517, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': -1, 'selected': False, 'text': '<p>Yes and no. You would end up releasing the string memory but "leaking" the NSAutoreleasePool object into memory by using drain instead of release if you ran this under a garbage collected (not memory managed) environment. This "leak" simply makes the instance of NSAutoreleasePool "unreachable" like any other object with no strong pointers under GC, and the object would be cleaned up the next time GC runs, which could very well be directly after the call to <code>-drain</code>:</p>\n\n<blockquote>\n  <p>drain</p>\n  \n  <p>In a garbage collected environment, triggers garbage collection if memory allocated since last collection is greater than the current threshold; otherwise behaves as release.\n  ...\n  In a garbage-collected environment, this method ultimately calls <code>objc_collect_if_needed</code>.</p>\n</blockquote>\n\n<p>Otherwise, it\'s similar to how <code>-release</code> behaves under non-GC, yes. As others have stated, <code>-release</code> is a no-op under GC, so the only way to make sure the pool functions properly under GC is through <code>-drain</code>, and <code>-drain</code> under non-GC works exactly like <code>-release</code> under non-GC, and arguably communicates its functionality more clearly as well.  </p>\n\n<p>I should point out that your statement "anything called with new, alloc or init" should not include "init" (but should include "copy"), because "init" doesn\'t allocate memory, it only sets up the object (constructor fashion). If you received an alloc\'d object and your function only called init as such, you would not release it:</p>\n\n<pre><code>- (void)func:(NSObject*)allocd_but_not_init\n{\n    [allocd_but_not_init init];\n}\n</code></pre>\n\n<p>That does not consume any more memory than it you already started with (assuming init doesn\'t instantiate objects, but you\'re not responsible for those anyway).</p>\n'}, {'answer_id': 68059, 'author': 'kperryua', 'author_id': 72126, 'author_profile': 'https://Stackoverflow.com/users/72126', 'pm_score': 3, 'selected': False, 'text': "<p>No, you're wrong. The documentation states clearly that under non-GC, -drain is equivalent to -release, meaning the NSAutoreleasePool will <strong>not</strong> be leaked.</p>\n"}, {'answer_id': 181043, 'author': 'mmalc', 'author_id': 23233, 'author_profile': 'https://Stackoverflow.com/users/23233', 'pm_score': 5, 'selected': False, 'text': '<h2>NSAutoreleasePool: drain vs. release</h2>\n\n<p>Since the function of <code>drain</code> and <code>release</code> seem to be causing confusion, it may be worth clarifying here (although this is covered in <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html#//apple_ref/occ/instm/NSAutoreleasePool/drain" rel="noreferrer">the documentation</a>...).</p>\n\n<p>Strictly speaking, from the big picture perspective <code>drain</code> is <em>not</em> equivalent to <code>release</code>:</p>\n\n<p>In a reference-counted environment, <code>drain</code> does perform the same operations as <code>release</code>, so the two are in that sense equivalent. To emphasise, this means you do <em>not</em> leak a pool if you use <code>drain</code> rather than <code>release</code>.</p>\n\n<p>In a garbage-collected environment, <code>release</code> is a no-op.  Thus it has no effect.  <code>drain</code>, on the other hand, contains a hint to the collector that it should "collect if needed".  Thus in a garbage-collected environment, using <code>drain</code> helps the system balance collection sweeps.</p>\n'}, {'answer_id': 7991256, 'author': 'Neovibrant', 'author_id': 655292, 'author_profile': 'https://Stackoverflow.com/users/655292', 'pm_score': 4, 'selected': False, 'text': '<p>As already pointed out, your second code snippet is correct.</p>\n\n<p>I would like to suggest a more succinct way of using the autorelease pool that works on all environments (ref counting, GC, ARC) and also avoids the drain/release confusion:</p>\n\n<pre><code>int main(void) {\n  @autoreleasepool {\n    NSString *string;\n    string = [[[NSString alloc] init] autorelease];\n    /* use the string */\n  }\n}\n</code></pre>\n\n<p>In the example above please note the <strong>@autoreleasepool</strong> block. This is documented <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 21909235, 'author': 'Gagan_iOS', 'author_id': 3173478, 'author_profile': 'https://Stackoverflow.com/users/3173478', 'pm_score': 0, 'selected': False, 'text': '<p>What I read from Apple:\n"At the end of the autorelease pool block, objects that received an autorelease message within the block are sent a release message—an object receives a release message for each time it was sent an autorelease message within the block."</p>\n\n<p><a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html" rel="nofollow">https://developer.apple.com/library/mac/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html</a></p>\n'}, {'answer_id': 24452608, 'author': 'Hardik Mamtora', 'author_id': 2125928, 'author_profile': 'https://Stackoverflow.com/users/2125928', 'pm_score': 0, 'selected': False, 'text': '<p>sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is drained (it may be longer if the object is subsequently retained). An object can be put into the same pool several times, in which case it receives a release message for each time it was put into the pool.</p>\n'}]
['https://Stackoverflow.com/questions/65427', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7979/']