{"id":609,"date":"2010-05-26T21:43:35","date_gmt":"2010-05-27T02:43:35","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=609"},"modified":"2010-05-26T21:43:37","modified_gmt":"2010-05-27T02:43:37","slug":"drain-an-nsautoreleasepool-dont-release-it","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2010\/05\/26\/drain-an-nsautoreleasepool-dont-release-it\/","title":{"rendered":"drain an NSAutoReleasePool Don&#8217;t release it"},"content":{"rendered":"<p><strong>To clean up an <code>NSAutoreleasePool<\/code>, do <code>[pool <a href=\"http:\/\/developer.apple.com\/iphone\/library\/DOCUMENTATION\/Cocoa\/Reference\/Foundation\/Classes\/NSAutoreleasePool_Class\/Reference\/Reference.html#\/\/apple_ref\/occ\/instm\/NSAutoreleasePool\/drain\">drain<\/a>];<\/code> <em>not<\/em> <code>[pool release];<\/code><\/strong><\/p>\n<p>In a garbage-collected environment, sending any object a <code>release<\/code> message is <a href=\"http:\/\/www.friday.com\/bbum\/2009\/12\/18\/objc_msgsend-tour-part-3-the-fast-path\/\">hardcoded by the runtime<\/a> to do nothing (very quickly). So <code>[pool release]<\/code> won&#8217;t do anything. But <code>[pool drain]<\/code> will signal the garbage collector to cleanup, and works correctly (just like <code>release<\/code>) in a non-garbage-collected environment.<\/p>\n<h3>Why This Still Matters on an iPhone<\/h3>\n<p>The iPhone doesn&#8217;t have garbage collection today. That doesn&#8217;t mean it never will. RIM and Android both support some kind of garbage collection. I&#8217;m too grizzled an Apple developer to not future proof my code, because I&#8217;ve been effected by Apple making some major runtime changes (eg. switching between PowerPC, x86, x86_64, and ARM processors). Section 3.3.1 of the iPhone SDK agreement means Apple&#8217;s runtime is the only game in town. It pays to be sure your code <em>always<\/em> plays nicely with it.<\/p>\n<p>Using <code>drain<\/code> also helps your code will play nice with Mac OS X. That gives you more options to re-use and monazite it. If you decide to go the open-route, it means more people will be able to use your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To clean up an NSAutoreleasePool, do [pool drain]; not [pool release]; In a garbage-collected environment, sending any object a release message is hardcoded by the runtime to do nothing (very quickly). So [pool release] won&#8217;t do anything. But [pool drain] will signal the garbage collector to cleanup, and works correctly (just like release) in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203,3,5,4],"tags":[241,581,224,580],"class_list":["post-609","post","type-post","status-publish","format-standard","hentry","category-iphone","category-macosx","category-objective-c","category-programming","tag-best-practices","tag-garbage-collection","tag-memory-management","tag-nsautoreleasepool"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=609"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/609\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/609\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}