iphoneobjective-ciosmemory-managementviewdidunload

Proper use of viewDidUnload


I know that similar questions have been asked before. But I've been searching SO for some time now and things are still a bit confusing. So here goes...

I am not using ARC. If you have a viewcontroller with an instance variable and a property like below:

ViewController.h:

@interface ViewController : UIViewController{
    NSDictionary *someDict;
}
@property(nonatomic, retain)UIView *someView;
@property(assign)UIView *someOtherView;

ViewController.m:

-(void)viewDidUnload{
    self.someView = nil;
    [someDict release];
    [someOtherView release];
    super viewDidUnload];
} 

Is this the correct way to implement viewDidUnload? Setting someDict = nil seems wrong as it will leak hence my guess is release. The same applies to someOtherView as it is not retained?

Am I wrong here? Thankful for any help!


Solution

  • self.someView = nil will not leak since it equals to [self setSomeView:nil] which is generated automatically by property-synthesize pair of @property(nonatomic, retain)UIView *someView. It has retain attribute so the retained object will be released when a new object is set.

    I believe [someDict release]; should be in dealloc. And [someOtherView release]; shouldn't be called since it is an assign property which doesn't have ownership.

    More references: