I have subclassed UIViewController 3 times. Should each one have [super viewDidUnload] in it?
Hopefully a simple question, and not too daft! Thanks.
- (void)viewDidUnload
{
...
[super viewDidUnload]; //<<< In all 3 subclasses?
}
Yes, you should call super in each class.
Note that this method is deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.
You should call [super viewDidUnload] in viewDidUnload method call.
You should read the ios dev document (memory management subsection). I am sure it will help you to understand the answer of your question ;)
Related
I have these 3 classes:
#interface BaseViewController : UIViewController
#interface BaseWebViewController : BaseViewController
#interface SecondaryWebViewController : BaseViewController
In AppDelegate, I am setting the rootViewController to be BaseWebViewController which will never be de-alloced throughout the entire lifetime of the app. In BaseWebViewController, I am calling [super viewDidLoad] and [super dealloc] in their respective methods.
Throughout most of the app, the user will be navigating through the UIWebView found on BaseWebViewController. But on certain links, I need to push (and later, pop out of) SecondaryWebViewController.
My question is that since both BaseWebViewController and SecondaryWebViewController are subclassing the BaseViewController, I'm not sure what to do regarding calling [super viewDidLoad] and [super dealloc] in SecondaryWebViewController.
In SecondaryWebViewController, I had been calling those [super viewDidLoad] and [super dealloc], but I noticed it caused some problems especially with random crashes when calling [super dealloc]. Since BaseWebViewController must always be present throughout the entire lifetime of the app, I don't ever want dealloc called on the BaseViewController.
Sorry, if this sounds confusing, but essentially, the flow looks like this:
AppDelegate sets rootViewController to BaseWebViewController
BaseWebViewController's viewDidLoad also calls the superclass BaseViewController's
viewDidLoad
Majority of the lifetime of the app is spent in
BaseWebViewController, but occassionally SecondaryWebViewController
needs to be pushed
SecondaryWebViewController's viewDidLoad and dealloc calls the superclass's viewDidLoad and dealloc causing strange behaviour.
Currently, if I comment out [super viewDidLoad] and [super dealloc] in SecondaryWebViewController so that BaseViewController is never dealloced, then everything runs fine. However, I don't know if this is the right way of doing things.
What is the approach of calling super functions in a subclass such as this scenario?
If I understood you correctly you are mixing the concepts of Classes and Objects.
By your description, you have two view controllers objects in your app. One of them is an instance of the BaseWebViewController class and the other is an instance of the SecondaryWebViewController class.
These two instances are completely separate from each other.
When your instance of SecondaryWebViewController calls dealloc, when it calls [super dealloc] this just invokes the dealloc method of the BaseWebViewController class - On the same instance. The other view controller instance is completely unaffected.
I might have completely misunderstood, and in that case please excuse me. If I did understand correctly, you should look up the concepts of classes and objects, this is really important basis for going on further.
It's been some time since I created a new project in Xcode.
Today I created a new "tabbed Application" and I noticed that the firstViewController and secondViewController created by Xcode don't have a dealloc method.
Why is that? do I have to create it myself?
Yes, the -dealloc always needs to be implemented by you. It doesn't exist by default regardless of template type.
After writing it yourself a few times, you will have it memorized...
In Xcode 4, control option command 2 brings up a code snippet library. You can find dealloc and other generated code you may have gotten used to in the past
- (void)dealloc {
//deallocations
[super dealloc];
}
yes you have to write it down by yourself
- (void)dealloc {
//deallocations
[super dealloc];
}
I know for sure that we can't but what would be exact explanation for this.
Perhaps these may help you
Calling dealloc in init?
[self release], [self dealloc] or [super dealloc] in init methods?
Difference between release and dealloc in objective-c
I am finishing up an iPad application, and for me, that means filling in all of the dealloc methods in my classes.
I have many views in my nib and most of them contain subviews (i.e. UIViews, UILabels, etc.). Should I release the child views before releasing the parent? If the parent view is released, will its child views be released, as well?
Sample:
-(void)dealloc
{
[super dealloc];
[childView release]; // Do I need this if I use the next line?
[parentView release]; // Will this perform the line above?
}
Both childView and parentView are properties of my view controller. Both have been retained.
Anything that you have retained (whether explicitly or through a retained property) needs to be released for the memory management to be balanced.
Also, you should invoke [super dealloc] at the end of your dealloc implementation, not the beginning (for instance, because you might depend on superclass resources still being available at the time).
By the way, saving your dealloc-writing until you're finishing up work on the app is a backwards way to go. I know it seems like house-cleaning work, but the fact is until you're managing memory properly, you're going to have a very skewed view of how your app really performs.
Ideally you should write your dealloc calls as you write your #synthesize statements. In other words, you add a property to your class, you set it up as a retained property, you write its #synthesize and -release code. That way you know your memory management is basically clean (I mean, at the class property level anyway) and you can code with confidence.
Since you specified that you have subviews from a NIB, it sounds like you also may need to pay close attention to the viewDidUnload method.
Any views which are automatically allocated from the nib you can implicitly release by setting the outlet to nil. For example:
- (void)viewDidUnload {
[super viewDidUnload];
self.subviewOutletOne = nil;
self.subviewOutletTwo = nil;
}
Then for any objects which you explicitly retain, you release them in the dealloc method like you are planning to do:
- (void)dealloc {
[myDataArray release];
[coolAnimatedImage release];
[myCustomSubview release];
[super dealloc];
}
Also be sure to check out the LEAKS instrument. This is a random tutorial for using the built in leak analysis tool. There may be others / better ones. It can a pain to get up and running the first time but is completely worth it.
What is the use of viewDidUnload and didReceiveMemoryWarning methods?
When they actually get called?
what are the difference between dealloc, viewDidUnload and didrecievedmemorywarning?
viewDidUnload: It is called when viewcontroller recieves the low memory warning.
dealloc: It is called when the object/viewController is released.
didRecieveMemorywarning: Also called when the controller recieves the low memory warning.
So Whats the difference between viewDidUnload and didrecieveemoryWarning?
viewDidUnload is the place where you need to clean up the UI related things i.e., outlets.
didRecieveMemoryWarning is a place where you need to clean up the other objects which are holding memory and not used frequently.
viewDidUnload is supposed to undo what viewDidLoad does, just like dealloc is supposed to free up whatever resources init created. So:
-(id)init {
if (self == [super init]) {
foo = [[Foo alloc] init];
}
return self;
}
-(void)dealloc {
[foo release];
[super dealloc];
}
-(void)viewDidLoad {
[super viewDidLoad];
bar = [[Bar alloc] init];
}
-(void)viewDidUnload {
[bar release];
[super viewDidUnload];
}
didReceiveMemoryWarning is there so you can free up any unnecessary memory. Perhaps you cache images. That's nice to have, for a snappy UI, but when memory's tight you can release that memory and your application's performance can degrade gracefully.
It's a good idea to start with Apple's documentation, as there is a lot there about this subject.
viewDidUnload is called when a UIViewController subclasses unloaded it's view. The view is then released, and all IBOutlets are disconnected. The method is usually fired when a viewController is popped, or when it recieves a memory warning.
didRecieveMemorywarning is called when the OS sends a memory warning. This happens when the device is low on memory. You should try a release as much data as possible here, to free up memory. Caches, data that is not needed at the moment, etc.
If you don't free up memory, your app will be killed at a certain point to free up memory for the OS.
Dealloc is called when the instance is deallocated.