1

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!

Hailei
  • 42,163
  • 6
  • 44
  • 69
TompaLompa
  • 949
  • 6
  • 17

2 Answers2

2

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:

Community
  • 1
  • 1
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • Thanks for answering! The first one i was quite sure about, but the other two? Any leads? – TompaLompa Apr 23 '12 at 16:42
  • `someDict` is a member of your class, it shouldn't be released until the instance will be destroyed. `viewDidUnload` is not the correct place. For `someOtherView`, it looks like a weak reference to another view, that's the reason I said that. – Hailei Apr 23 '12 at 16:47
  • More references added in my answer. – Hailei Apr 23 '12 at 16:53
1

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.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • so you mean that if the dictionary someDict was a view it should have been released as it was not a retained property? and the same for the someOtherView? – TompaLompa Apr 23 '12 at 16:52
  • I took your advice on getting rid of all ivars, but that raises a question. What about inheritance? – TompaLompa Apr 23 '12 at 17:42