{"id":601,"date":"2010-05-19T22:25:48","date_gmt":"2010-05-20T03:25:48","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=601"},"modified":"2010-05-19T22:25:50","modified_gmt":"2010-05-20T03:25:50","slug":"n-a-r-c","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2010\/05\/19\/n-a-r-c\/","title":{"rendered":"N.A.R.C."},"content":{"rendered":"<p>How to remember Cocoa memory management:<\/p>\n<blockquote><p>Think <strong>NARC<\/strong>: &#8220;New Alloc Retain Copy&#8221;. If you are not doing any of those things, you don&#8217;t need to <code>release<\/code>.<\/p><\/blockquote>\n<p>&#8211;Andiih <a href=\"http:\/\/stackoverflow.com\/questions\/2865185\/do-you-need-to-release-parameters-of-methods-at-the-end-of-them-in-objective-c\">on Stack Overflow<\/a><\/p>\n<p>Personally, I like to <em>immediately<\/em> <code>autorelease<\/code> anything I NARC-ed, on the same line. For example:<\/p>\n<pre>Foo* pityTheFoo = [[[Foo alloc] init] autorelease];<\/pre>\n<p>Admittedly, this makes for some ugly, bracey, lines. But I think it&#8217;s worth it, because you <em>never<\/em> having to worry about calling <code>release<\/code> if you also&#8230;<\/p>\n<h3>Use a <code>@property<\/code> (or Setter) Instead of <code>retain<\/code><\/h3>\n<p>In other words I would write an <code>init<\/code> method that looked like:<\/p>\n<pre>\n- (id) init\n{\n\tself = [super init];\n\tif (self) {\n\t\t_ivar = [[Foo alloc] init];\n\t}\n\treturn self;\n}<\/pre>\n<p>as:<\/p>\n<pre>\n- (id) init\n{\n\tself = [super init];\n\tif (self) {\n\t\tself._ivar = [[[Foo alloc] init] autorelease];\n\t}\n\treturn self;\n}\n<\/pre>\n<p>(Or <code>[self setIvar:[[[Foo alloc] init] autorelease]];<\/code> if you are one of those folks who hate the dot-syntax.)<\/p>\n<p>It&#8217;s  debatable if <a href=\"http:\/\/www.mikeash.com\/pyblog\/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html\">using acessors in <code>init<\/code> and <code>dealloc<\/code><\/a> is a good idea. I even left a comment on that post arguing against it. But since then I&#8217;ve done a lot of reflection, and in my experience using a <code>@property<\/code> instead of an explicit <code>release<\/code>\/<code>= nil<\/code> solves more problems then it causes. So I think it&#8217;s the best practice.<\/p>\n<p>Even if you disagree with me on that point, if <strong>the only places you explicitly NARC objects are <code>init<\/code>, <code>dealloc<\/code>, and <code>setX:<\/code> methods<\/strong> then I think you&#8217;re doing the right thing.<\/p>\n<h3>Cycles!<\/h3>\n<p>The last piece of the memory-management puzzle are <a href=\"http:\/\/www.mikeash.com\/pyblog\/friday-qa-2010-04-30-dealing-with-retain-cycles.html\">retain cycles<\/a>. By far the best advice I&#8217;ve seen on them is  <a href=\"http:\/\/www.mikeash.com\/pyblog\/friday-qa-2010-04-30-dealing-with-retain-cycles.html\">Mike Ash&#8217;s article<\/a>. Read it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to remember Cocoa memory management: Think NARC: &#8220;New Alloc Retain Copy&#8221;. If you are not doing any of those things, you don&#8217;t need to release. &#8211;Andiih on Stack Overflow Personally, I like to immediately autorelease anything I NARC-ed, on the same line. For example: Foo* pityTheFoo = [[[Foo alloc] init] autorelease]; Admittedly, this makes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,203,3,5,4,10],"tags":[376,241,224,375],"class_list":["post-601","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-iphone","category-macosx","category-objective-c","category-programming","category-tips","tag-autorelease","tag-best-practices","tag-memory-management","tag-retain"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/601","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=601"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions\/602"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}