1

Hi I have this in my application : The question is, should I put self.label =nil; in viewDidUnload? If yes, why?

//.h
@interface MyClass

@property (nonatomic, retain) IBOutlet UILabel *label;

@end

//.m
@implementation Myclass

@syntesize label = label_;

- (void)dealloc
{
   self.label =nil;
}

@end
jrouquie
  • 4,315
  • 4
  • 27
  • 43
samir
  • 4,501
  • 6
  • 49
  • 76

4 Answers4

3

Yes, you should set the label property to nil both in viewDidUnload and in dealloc. viewDidUnload is called in low memory situations which enables the app to purge unneeded memory.

Not setting it to nil in viewDidUnload will not usually cause a memory leak in, but it will prevent the app from saving memory when needed.

Anton
  • 5,932
  • 5
  • 36
  • 51
  • 19
    Note that Apple has now officially deprecated `-viewDidUnload`, even when writing code for pre-iOS 6 apps. You should now do everything in `didReceiveMemoryWarning`. See the various WWDC 2012 videos for more information. – Mike Weller Jul 04 '12 at 13:04
  • @MikeWeller Good news to me. Thanks for share. +1. Could I insert your comment in my answer to allow other people to be aware of this new thing? – Lorenzo B Jul 04 '12 at 13:08
  • @MikeWeller I didn't know that, but if it's officially deprecated even for pre-iOS 6 code, why hasn't the UIViewController documentation been updated to reflect this? Thanks for clarifying though. – Anton Jul 04 '12 at 13:10
  • This information is fresh out of WWDC so it will take time for the documentation to be updated. It will probably only come with the official iOS 6 launch. Now that I've said all this, I'm wondering if this information is under NDA. – Mike Weller Jul 04 '12 at 13:12
  • @MikeWeller I believe this is still under NDA until iOS 6 is out of beta as this would be considered an "implementation detail" of the iOS 6 beta SDK. The iOS SDK documentation will be updated to reflect these changes once it is no longer under NDA.In the future, if the documentation you're reading shows "prerelease" in the URL, it's under NDA and should not be disclosed outside of the beta forums. – Beltalowda Sep 06 '12 at 17:41
3

You should do.

viewDidUnload is called in low memory condition. So if you want to clean up call self.yourOutlet = nil also in this method. Furthermore it allows you to save extra memory for your app.

The next time (after viewDidUnload method is called) your view will be loaded into memory again (viewDidLoad will be called) and your outlet will be set up correctly.

As a rule of thumb any IBOutlets you release in dealloc, should also be released (reference set to nil like self.label = nil) in this method.

A note

You should not call self.label = nil; in dealloc. Instead do [label_ release]; as documented in Apple Memory Management Guide.

In addition, Stack Overflow search is your friend:

When is UIViewController viewDidUnload called?

When should I release objects in -(void)viewDidUnload rather than in -dealloc?

Hope that helps.

Edit

if you not use ARC (I think not) you should call also [super dealloc]; like the following:

- (void)dealloc
{
   [label_ release];

   [super dealloc];
}
Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • "if you not use ARC (I think not) you should call also" well, if you were using ARC, you wouldn't have either of those statements there – user102008 Oct 30 '12 at 23:41
1

You should. Although not necessary in most cases, it is considered good practice to set all your pointers to objects to nil on viewDidUnload. Paul Hegarty explains that on CS193P lecture number 8, Controller Lifecycle.

You can watch it here: http://itunes.apple.com/itunes-u/ipad-iphone-application-development/id473757255?mt=2

Breno Gazzola
  • 2,092
  • 1
  • 16
  • 36
-2

You can put this, but you can also put [label release], self.label = nil; or just [label release];

It's memory management so that the memory reserved for that object be garbage collected [memory released]. Very important on old iphone 3g, because it has less memory for the user to run programs but minimal in newer iphone 4+/ios 5.x since it uses arc and you can skip that if you use ARC projects.

Panagiotis
  • 1,539
  • 1
  • 14
  • 28
  • 4
    Not really correct. [label release], self.label = nil; will cause the label to be released twice, and [label release]; will create a dangling pointer. self.label = nil or [label release], label = nil; would be better. – Anton Jul 04 '12 at 12:49