<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2012-04-13-nib-memory-management.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 17 Aug 2026 10:28:39 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>haipret - 2015-07-10 08:18:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>Why objects in storyboard that are attached to scene of view controller are deinit with view controller deinit even if they are not view controller's view subviews and are not outlet-ed to the view controller?</description><guid isPermaLink="true">79dddb960d4fddd5ef740e809b988ca1</guid><pubDate>Fri, 10 Jul 2015 08:18:51 GMT</pubDate></item><item><title>Jul - 2013-06-13 16:57:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>For iOS using ARC, if I set the property as strong for 1 of my subviews, and do not set it to nil in -didReceiveMemoryWarning (or viewDidUnload when it wasn't deprecated), will it leak or cause some kinds of problem? If yes, please explain. If not, then is there any downside for using @property (strong) for those outlets?</description><guid isPermaLink="true">bf61c2c23adc20fa9e5ce972a9c76d03</guid><pubDate>Thu, 13 Jun 2013 16:57:39 GMT</pubDate></item><item><title>Ari Braginsky - 2012-10-18 17:04:06</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>Note: with iOS 6 viewDidUnload is now deprecated.  I guess if you're using strong for IBOutlet properties and are concerned with low memory conditions, you can still set them to nil in -didReceiveMemoryWarning.</description><guid isPermaLink="true">9fb1e032a968efb1272326a9acc90110</guid><pubDate>Thu, 18 Oct 2012 17:04:06 GMT</pubDate></item><item><title>hankbao - 2012-06-19 05:48:06</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>There is actually a special scene in iOS.
&lt;br /&gt;
&lt;br /&gt;If you got some top-level objects in your MainNib, they are not autoreleased and will remian in memory except you release them explicitly.
&lt;br /&gt;
&lt;br /&gt;In some circumstance, this may cause a leak.</description><guid isPermaLink="true">beb63c7918240d9ec77514da08851fd9</guid><pubDate>Tue, 19 Jun 2012 05:48:06 GMT</pubDate></item><item><title>Jorge - 2012-05-04 19:57:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>A small comment i would add to the article would be something like...
&lt;br /&gt;
&lt;br /&gt;'When the NIB gets loaded, 'viewDidLoad' method gets called... and... if for whatever reason you need to force the ViewController to load a nib file during the init... the common hack is to simply call [self view]'.
&lt;br /&gt;
&lt;br /&gt;I don't like that practise, but it has been handy in several occasions.
&lt;br /&gt;
&lt;br /&gt;Thanks for sharing this!</description><guid isPermaLink="true">d14d20701aed725d61f260b8eb168c9a</guid><pubDate>Fri, 04 May 2012 19:57:39 GMT</pubDate></item><item><title>mikeash - 2012-04-24 02:04:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>That article is interesting. It strikes me as a knee-jerk reaction from someone who is really used to the way things are done. I think that, taking a step back and looking at things objectively, the iOS way is much, much better. It's totally consistent and works the way you'd expect it to, whereas the Mac way of sort of doing kind of what KVC does but not quite means you end up with two different facilities that both search your objects for setters and ivars and manipulate them, but in subtly different ways.</description><guid isPermaLink="true">ce6912c9a7c75350d833f148db7d17c5</guid><pubDate>Tue, 24 Apr 2012 02:04:03 GMT</pubDate></item><item><title>Daniel - 2012-04-22 23:38:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>Funnily enough, I've just read Aaron Hillegass's "Real iPhone Crap, Part I" (&lt;a href="http://weblog.bignerdranch.com/?p=95"&gt;http://weblog.bignerdranch.com/?p=95&lt;/a&gt;) from back in 2009, where he argued that the use of KVC in NIB loading in UIKit (and thus inferring outlets to be strong) was a bad idea — I'd love to know whether or not he changed his opinion about this!
&lt;br /&gt;
&lt;br /&gt;Since I'd consider getting rid of the view without disposing of the view controller on the Mac as very uncommon, (and I think the absence of a viewDidUnload is a pretty good indicator for that) I don't really understand how he arrived at that conclusion anyways...</description><guid isPermaLink="true">58b06d99610998b268e619668700d9df</guid><pubDate>Sun, 22 Apr 2012 23:38:08 GMT</pubDate></item><item><title>mikeash - 2012-04-17 16:53:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>&lt;b&gt;Sean M:&lt;/b&gt; The situation is really easy under GC: make outlets, use them, and don't worry about it. GC is very nice when it works....
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Andrew Bonventre:&lt;/b&gt; The reasons for being dubious about calling setters in init/dealloc is not simply because the setters may be overridden, but because you'd end up calling those overridden setters on a partially constructed/destructed object. That's not a problem in something like viewDidUnload. In fact, not only is it acceptable to call the setter there, it's really what you should do: screwing with ivars directly instead of using their accessors is generally a big no-no. init/dealloc are exceptions to this rule, but viewDidUnload definitely is not.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Rob C. Grant:&lt;/b&gt; Yes, it's fine to use a UIViewController and then do stuff with its view. You'll probably want to subclass UIViewController to add code to control the reusable view element, then keep that controller around and manipulate it from your higher level controller to fiddle with the view in question.</description><guid isPermaLink="true">f3f0ab823b649204229d490de6dee056</guid><pubDate>Tue, 17 Apr 2012 16:53:14 GMT</pubDate></item><item><title>Rob C. Grant - 2012-04-14 21:57:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>I'm unclear on the implications of this article re: nibs that contain reusable interface elements. In such a case, is it appropriate to use a UIViewController to load the nib and then add the controller's view as a subview in some other view controller's hierarchy?</description><guid isPermaLink="true">52be6284ed968866ece75fff438c6afc</guid><pubDate>Sat, 14 Apr 2012 21:57:25 GMT</pubDate></item><item><title>Andrew Bonventre - 2012-04-14 14:36:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>A tiny digression but this post sparked the question in my brain again...
&lt;br /&gt;
&lt;br /&gt;Apple discourages dot syntax in init/dealloc but encourages &lt;code&gt;self.myOutlet = nil;&lt;/code&gt; in viewDidUnload. What's the best convention, here?
&lt;br /&gt;
&lt;br /&gt;I understand the reasoning behind not using &lt;code&gt;self.bloop = blah;&lt;/code&gt; within the init and dealloc methods because of the possibility of those setter methods being overridden in a subclass amongst other reasons.
&lt;br /&gt;
&lt;br /&gt;Is it simply for convenience that they encourage &lt;code&gt;self.outlet = nil;&lt;/code&gt; within the boilerplate comments on every UIViewController's viewDidUnload method? What's your preferred method and why?</description><guid isPermaLink="true">942c902e0b1575ef13b50d086df6e6d4</guid><pubDate>Sat, 14 Apr 2012 14:36:13 GMT</pubDate></item><item><title>Matthijs Hollemans - 2012-04-14 03:43:33</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>On iOS with ARC, Apple now recommends making weak outlets for all subviews, but any top-level views besides self.view should have strong outlets as there is nothing but these outlets keeping those objects alive. 
&lt;br /&gt;
&lt;br /&gt;Using weak for subviews has the advantage that your viewDidUnload methods hardly have to do any work anymore, since the weak outlets will be automatically unloaded for you when self.view disappears.
&lt;br /&gt;
&lt;br /&gt;However, if the nib contains any other top-level views (that you made strong outlets for), then these properties still need to be "nilled out" in viewDidUnload.</description><guid isPermaLink="true">9f1c20c925b6f3e5f44a4e70ad9897bf</guid><pubDate>Sat, 14 Apr 2012 03:43:33 GMT</pubDate></item><item><title>Sean M - 2012-04-14 03:28:02</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>I guess you omit garbage collection since it's basically deprecated now? Shame, because it would seem to be the easiest case of all. Reading the doc Avi linked to reminds me of how much I'll miss GC! :(</description><guid isPermaLink="true">5afedcf2b0de0036da402546a5bee3e3</guid><pubDate>Sat, 14 Apr 2012 03:28:02 GMT</pubDate></item><item><title>mikeash - 2012-04-13 19:02:30</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>Since top-level objects in iOS nibs aren't retained by the nib loader, they'll be destroyed as soon as everybody stops expressing interest in them. If the only connection to them is an assign or weak outlet, they'll be destroyed immediately once the nib is done loading, probably not what you want.</description><guid isPermaLink="true">be6f889b8f903d1a4b03a2bf9d3c5853</guid><pubDate>Fri, 13 Apr 2012 19:02:30 GMT</pubDate></item><item><title>Eimantas - 2012-04-13 18:29:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>"weak can be a good choice for outlets to subviews on iOS"
&lt;br /&gt;
&lt;br /&gt;Does this mean I can have `assign` type properties, in non-ARC environment, to subviews of top level objects when loading nibs on iOS so that I don't need to release them in viewDidUnload and dealloc methods?</description><guid isPermaLink="true">18500b43b212c690b2110ef05bae3e5e</guid><pubDate>Fri, 13 Apr 2012 18:29:35 GMT</pubDate></item><item><title>mikeash - 2012-04-13 15:45:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>You still have to release the objects when using ARC, ARC just makes it really hard to do so. Yet another reason you should always use a Cocoa controller to load nibs instead of doing it yourself.</description><guid isPermaLink="true">37b745b1cdd05310ab70b723301a473c</guid><pubDate>Fri, 13 Apr 2012 15:45:58 GMT</pubDate></item><item><title>Avi Drissman - 2012-04-13 14:52:56</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>"Release each top-level object to balance the alloc sent when loading the nib"
&lt;br /&gt;
&lt;br /&gt;Unless you're using ARC. From &lt;a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW18"&gt;https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW18&lt;/a&gt; :
&lt;br /&gt;
&lt;br /&gt;"If the File’s Owner is not an instance of NSWindowController or NSViewController, then you need to decrement the reference count of the top level objects yourself. With manual reference counting, it was possible to achieve this by sending top-level objects a release message. You cannot do this with ARC. Instead, you cast references to top-level objects to a Core Foundation type and use CFRelease."
&lt;br /&gt;
&lt;br /&gt;(sob)</description><guid isPermaLink="true">3b44a6cc6693ca5f376bcaca43807885</guid><pubDate>Fri, 13 Apr 2012 14:52:56 GMT</pubDate></item><item><title>Remy "Psy" Demarest - 2012-04-13 14:37:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-04-13-nib-memory-management.html#comments</link><description>In iOS, top-level objects and other objects are not initialized with a class-specific init.
&lt;br /&gt;There are two ways, either the type of object is known by IB (you didn't pull a NSObject cube into the nib), then the object is always initialized using -initWithCoder:, if the object is not known, then it uses -init only.</description><guid isPermaLink="true">453bd3da1246139a309bbc21223bdc50</guid><pubDate>Fri, 13 Apr 2012 14:37:42 GMT</pubDate></item></channel></rss>
