correct example:
- (void)dealloc { [viewController release]; [window release]; [super dealloc]; }
wrong example:
- (void)dealloc { [super dealloc]; [viewController release]; [window release]; }
Althoug in alsmost all other cases when overriding a method I would first call the super's method implementation, in this case apple always calls [super dealloc] in the end. Why?
Here's the answer to that questions.... Basically, super() is something that must be called if you're overriding something that MUST be called, which can often be rather complicated code. Now, the reason they don't just do it for you and call it before your function is mostly so that you have control! :-D.
However, using super() is not compulsory. Even if super() is not used in the subclass constructor, the compiler implicitly calls the default constructor of the superclass.
Based on iOS documentation and some discussions on the Internet, it would appear that viewDidLoad is called after view is loaded into memory and it's purpose is to allow for some custom setup of views loaded from nib files (basically a quote from docs). Apple documentation does not mention that you should call super.
The super keyword is used to call the constructor of its parent class to access the parent's properties and methods. Tip: To understand the "inheritance" concept (parent and child classes) better, read our JavaScript Classes Tutorial.
Its just a guideline. You can call other instructions after [super dealloc]
. however you can not access variables of the superclass anymore because they are released when you call [super dealloc]
. It is always safe to call the superclass in the last line.
Also KVO and depended (triggered) keys can produce side effects if they are depended of already released member variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With