Question

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!

Was it helpful?

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:

OTHER TIPS

In viewDidUnload you should release and nil all views that are retained subviews of your view controller's main view (i.e. not dictionaries!). It's that simple.

So your

self.someView = nil;

is correct, just add all your other retained subviews in to the method as well.

Don't release someOtherView, as this is an assigned property. Releasing it will cause your app to crash.

And if I were you, I'd get rid of the iVar declarations (someDict), and use all properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top