Do I need to add the dealloc method to the Objective-C class?

If you create a subclass of UIviewController, the dealloc method is automatically created for you.

- (void)dealloc{} 

However, when I create the Objective-C class, the method is not automatically created. Do I need to add a dealloc method so that I can release properties in the class? Especially if I saved them in the header file?

For example, in my Objective-C class header file, I have

 @interface ClassA : NSObject { NSString *someProperty; UINavigationController *navcon; } @property (nonatomic, retain) NSString *someProperty; @property (nonatomic, retain) UINavigationController *navcon; 

Do I need to manually create the dealloc method to free the properties as shown below?

 -(void)dealloc { [someProperty release]; [navcon release]; } 
+4
source share
2 answers

Yes you should.

dealloc is called on your object before it is destroyed forever, and its memory is returned to the OS. If it rests on other objects, for example, in your case, it is also important that these objects are released.

That's why you need to redefine dealloc and free up any resource that you hold there.

The fact that some Xcode template may or may not provide you with an example implementation of dealloc does not matter.

+6
source

You are responsible for freeing all the top-level objects in the nib file of the controller file. -dealloc is the usual place to do this.

+1
source

All Articles