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
Related
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 ;)
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.
This is really weird in my perspective. I've never seen anything like it. I put all my releases in a method called releaseMethod. Then in my dealloc and didReceiveMemoryWarning, I have [self releaseMethod]; I do this to be more object orienteted and save code because I have a lot of releases. But, occasionally (2 out of 5 times, give or take), I get EXC_BAD_ACCESS on the releaseMethod call in dealloc. The code is below. I didn't know it was possible to have a method call get bad access. I understand memory management and there is no memory involved in calling a method, right?
Thanks in advance.
- (void)dealloc {
[super dealloc];
[self releaseMethod];
}
Put your [super dealloc] at the end of the dealloc so you can first cleanup things in your class before cleaning up things in the superclass (which you might depend on).
If you send the message release to an object that has already been deallocated, this is the message you will get. Check that you aren't overreleasing something in releaseMethod. Remember, when an object is deallocated, it will release objects that it is retaining.
You should also put [self releaseMethod] before you call [super dealloc].
I have a property defined as:
#property(nonatomic, retain) UITableView *settingsTableView;
Then in my viewDidLoad method I have:
self.settingsTableView = [[[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped] autorelease];
[self.view addSubview:self.settingsTableView];
[self.settingsTableView release];
Then in the dealloc method of the view controller I have:
[settingsTableView release];
When I attempt to do the release from within the dealloc I am getting a "message sent to deallocated instance". I am starting to second guess myself, anybody see anything stupid in what I've done?
Really appreciate the help on this one!
you're calling release on an object you've already autoreleased. Just get rid of the line
[self.settingsTableView release];
and you should be good.
Note that you should keep the release in the dealloc method, since the property calls retain for you, but not release.
Two things. First, you're over-releasing the table view in the first place: the autorelease call negates the need for a manual release afterwards.
Also, in general, what you release in -dealloc are things that you created in -init, -initWithCoder:, or whatever, not loadView or -viewDidLoad. In this case, the method you're looking for is -viewDidUnload; you just have to set self.settingsTableView to nil in that method, and the property setter will handle releasing it if necessary.
Here is the change you need to make.
self.settingsTableView = [[[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped] autorelease];
[self.view addSubview:self.settingsTableView];
[self.settingsTableView release];
//^^^ This line is bad no need to release this value until dealloc
//if it is defined as retain or copy
Seems obvious. You have already released with
[self.settingsTableView release];
So why release it again in the dealloc?
I believe the problem is related to your use of autorelease when you are allocating and initializing the UITableView.
You also might have a problem with releasing the settingsTableView right after you use it, vs. in the dealloc method. Anytime you alloc/init an object, you should only release it once.
If you use autorelease, the rules are a little different, so I'd recommend reading up on that again. Also, when you pass objects that you've created off to other things, they may take complete or shared ownership of the object by retaining it, meaning that you may or may not need to release it yourself anymore. The documentation for that should be on the method you're calling (like addSubView).
I am creating a subclass of a custom UITableViewCell. The superclass has some properties that it is releasing in the dealloc method. I have added one additional property in my subclass, so I made sure to override the dealloc method and release my new property.
My question is, since I overrode the dealloc method, will the properties that were released in super class dealloc method not get released (I AM calling [super dealloc])? Do I need to specifically release those in MY dealloc method?
If you are calling [super dealloc], then the superclass implementation of -dealloc will still be run. The superclass should be responsible for releasing its own properties. So no, you don't need to release the superclass properties. In fact, doing so will likely cause your application to crash.
If you are calling [super dealloc], you are fine. That method won't know if it was called by a subclass or directly by the runtime, and it will do its usual work and take care of its own properties as always.
Important: Call [super dealloc] last in your -dealloc method, after you've your own ivars and done anything else you need to do in -dealloc. There won't be anything left of your object when that method returns.