IOS: freeing a variable using @property (non-atomic, saving)

If variables with a non-automatic and persistent @property value are explicitly issued.

@interface MyScreenViewController : UIViewController <UIWebViewDelegate> { UIWebView* greetingView; } @property(nonatomic, retain) IBOutlet UIWebView* greetingView; - (void)dealloc { [greetingView release]; } 

Is a release required in the delloc method ???

+4
source share
2 answers

Ofcourse. YES, you must let go of your properties with saving, copying and not assigning. You set the Nil property to viewDidUnload.

 - (void)viewDidUnload { [super viewDidUnload]; self.greetingView = nil; } - (void)dealloc { [greetingView release],greetingView = nil; [super dealloc]; } 

Refer to memory management in the UIViewController help system: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Memory is a critical resource in iOS, and view controllers provide built-in support to reduce memory at critical times. The UIViewController class provides some automatic handling of low memory conditions using the didReceiveMemoryWarning method, which frees up unnecessary memory. Prior to iOS 3.0, this method was the only way to free up additional memory associated with your custom controller class, but in iOS 3.0 and later, the viewDidUnload method may be a more suitable place for most needs.

When a low-memory warning occurs, the UIViewController class clears its views if it knows that it can reload or recreate them later. If this happens, it also calls the viewDidUnload method to give your code the option to give up ownership of any objects associated with your view hierarchy, including objects loaded by the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and Added to view hierarchy. Typically, if your view controller contains output (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to give up ownership of these points or any other view-related data that you no longer need.

+5
source

Yes, you have to free him, because he is saved. Any property that is saved (or copied) must be issued by the same class (if assigned, it does not need to be freed).

Save basically as a class saying, "I'm going to use this other class, so keep it in mind." dealloc is the class that said that it itself would be deleted from memory. Therefore, if a class that needs your saved property is about to disappear, this object must also be freed.

Otherwise, he will simply sit there in his memory when he does not need anything in the program. And if that happens, you will run out of memory.

(Also make sure you call [super dealloc] at the end of your dealloc method.)

+2
source

All Articles