{"id":607,"date":"2010-05-25T15:32:19","date_gmt":"2010-05-25T20:32:19","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=607"},"modified":"2010-05-25T15:32:22","modified_gmt":"2010-05-25T20:32:22","slug":"write-dealloc-first","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2010\/05\/25\/write-dealloc-first\/","title":{"rendered":"Write dealloc FIRST"},"content":{"rendered":"<p>As soon as you give a class a new instance variable (ivar), update the class&#8217;s <code>dealloc<\/code> method (and <code>viewDidUnload<\/code>, if the ivar is an <code>IBOutlet<\/code>) to clean up the ivar. Do this <em>before<\/em> you write the code using the new ivar. Here&#8217;s why:<\/p>\n<h3>Never Forget<\/h3>\n<p>You can&#8217;t forget to <code>release<\/code> an ivar, if the code that reaps it is in place <em>before<\/em> the code that creates it. Updating <code>dealloc<\/code> first means less memory leaks.<\/p>\n<p>Even with an impossibly good testing protocol, that catches <em>every<\/em> memory leak, it&#8217;s faster to fix memory leaks before they happen than to track them down after the fact.<\/p>\n<h3>You Know More Than They Do<\/h3>\n<p>Sometimes there&#8217;s an important step that must be done when cleaning up an ivar. Maybe you need to set it&#8217;s <code>delegate<\/code> to <code>nil<\/code>, or unregister for a notification, or break a <a href=\"http:\/\/www.mikeash.com\/pyblog\/friday-qa-2010-04-30-dealing-with-retain-cycles.html\">retain cycle<\/a>. You know this when you setup the ivar. But your coworkers don&#8217;t know this <em>a priori<\/em>. When you checkin code that leaks or triggers an analyzer warning, they&#8217;ll want to fix it, and since they know less than you do about your code, they&#8217;re more likely to miss a crucial step. (Even if you work alone, remember Future You! In <em>N<\/em> weeks, Future You will have to deal with all the code Present You wrote today &#8230; and they&#8217;ll be in the same situation as any other co-worker, because they won&#8217;t remember everything Present You knows. )<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As soon as you give a class a new instance variable (ivar), update the class&#8217;s dealloc method (and viewDidUnload, if the ivar is an IBOutlet) to clean up the ivar. Do this before you write the code using the new ivar. Here&#8217;s why: Never Forget You can&#8217;t forget to release an ivar, if the code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,6,5,4],"tags":[241,579,578,224],"class_list":["post-607","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-cocoa","category-objective-c","category-programming","tag-best-practices","tag-dealloc","tag-ivars","tag-memory-management"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/607","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=607"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions\/608"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}