Responding to view controller memory warnings (was Re: Outlets / IBOutlet declarations)
Responding to view controller memory warnings (was Re: Outlets / IBOutlet declarations)
- Subject: Responding to view controller memory warnings (was Re: Outlets / IBOutlet declarations)
- From: mmalcolm crawford <email@hidden>
- Date: Fri, 21 Nov 2008 16:53:07 -0800
Context:
UIViewController provides a method, didReceiveMemoryWarning, which is
invoked on view controllers when the amount of memory available to the
application is severely constrained. The goal of the method is to
allow view controllers to dispose of resources that are currently not
needed and that can be recreated later if required. One such resource
is the view controller's view itself. Assuming that it does not have
a superview, the view is disposed of ([self setView:nil];).
A "issue" arises in that outlets to elements within the nib file are
typically declared as follows:
@property (nonatomic, retain) IBOutlet ElementClass *element;
Thus even though the main view is disposed of, absent any further
action the outlets are still retained. This is not in and of itself a
problem -- if and when the main view is reloaded, they will simply be
replaced -- but it does mean that the beneficial effect of the
didReceiveMemoryWarning is reduced.
There are, currently, a couple of possible remedies...
On Nov 19, 2008, at 12:59 AM, mmalcolm crawford wrote:
This leaves us for now with two solutions:
(a) Greg's (override setView:) which is more future-proof but is in
many respects academically unsatisfying.
- (void)setView:(UIView *)aView;
{
if (!aView) {
// set outlets to nil, e.g.
self.anOutlet = nil;
}
// Invoke super's implementation last
[super setView:aView];
}
Unfortunately, although in principle this is correct, in practice it
may fall foul of another issue.
Because UIViewController currently implements its dealloc method using
the setView: accessor method (rather than simply releasing the
variable directly...), self.anOutlet = nil will be called in dealloc
as well as in response to a memory warning... This will lead to a
crash in dealloc.
The remedy is to ensure that outlet variables are also set to nil in
dealloc:
- (void)dealloc {
// release outlets and set variables to nil
[anOutlet release], anOutlet = nil;
[super release];
}
The iPhone engineering team is aware of this issue (read: There is no
need to file bugs on this).
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden