Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to call super -dealloc last, and not first?

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?

like image 700
Thanks Avatar asked May 26 '09 09:05

Thanks


People also ask

Why is it important to call super () when overriding base activity events?

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.

Is it necessary to call super() inside a constructor?

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.

Why do we call super viewDidLoad?

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.

Why do we call super in constructor?

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.


1 Answers

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.

like image 145
cocoafan Avatar answered Sep 17 '22 16:09

cocoafan