views:

402

answers:

3
- (void)viewDidUnload {
  self.GPSArray = nil;
  self.accelerometerArray = nil;
  self.headingArray = nil;
  self.managedObjectContext = nil;
  self.locationManager = nil;
  self.pointLabel = nil;
  self.accelerometerLabel= nil;
  self.headingLabel= nil;
  self.startStop = nil;
  self.lastAccelerometerReading = nil;
  self.lastGPSReading = nil;
  self.lastHeadingReading = nil;
}


- (void)dealloc {
  [GPSArray release];
  [accelerometerArray release];
  [headingArray release];
  [managedObjectContext release];
  [locationManager release];
  [pointLabel release];
  [accelerometerLabel release];
  [headingLabel release];
  [startStop release];
  [lastAccelerometerReading release];
  [lastGPSReading release];
  [lastHeadingReading release];
  [super dealloc];  
}
A: 

You don't have to, and if you run this, you'll be wasting some substantial execution time. Using the property method to set a property to nil is the same thing as releasing the property, with the caveat that some extra stuff may or may not happen, depending on how you've set up the setter methods.

So let's walk through this code. At the end of your viewDidUnload method, all of your properties are now nil. The object is then deallocated, and your object attempts to release a dozen nil objects or so. Now, the Objective-C runtime is pretty smart, and if you send a message to nil, (surprise surprise) nothing will happen.

So you've basically got a dozen lines that do absolutely nothing.

Dave DeLong
If you're going to vote down an answer that's technically correct, it's usually helpful to leave a comment explaining why...
Dave DeLong
I wasn't the person that voted yours down, but I don't agree it's technically correct. viewDidUnload is called when a UIViewController's view is unloaded. It is not necessarily followed by a call to dealloc, and - depending on your app design - there's a good chance it won't be.
grahamparks
@grahamparks fair enough =). I almost never see this called except near deallocation, but I suppose if he gets a memory warning and decides to release the view, then this would fire.
Dave DeLong
A: 

no, you shouldn't.

if you do as if the above code, you are wasting effort.

By setting them to nil in viewDidUnload, they will be going thru a process of release and retain automatically, which means, by the time the code get to dealloc, they are actually released and nil, and you are doing another release there for the nil. Releasing nil object might be erratic.

So, ignore those in viewDidUnload.

Zteeth
why -1??can you explain to me why a vote down is given plz?
Zteeth
See the correct answer.
Andrew Johnson
+1  A: 

The reason viewDidUnload is called, is that your view is being released and any view resources should be freed.

So, you only need to free view related items.

In your case it looks like you'd only need to free the UILabels that are probably in your view. If they were marked as IBOutlets and not in assign properties, you'd want to release the memory used by them:

self.pointLabel = nil;
self.accelerometerLabel= nil;
self.headingLabel= nil;

That also means, that in viewDidLoad if you are setting up the other properties you want to make sure they are not being allocated again if they are there already as it can be called again if the view is unloaded and then reloaded again.

The reason this would be called is if the view controller received a memory warning. You can test this memory warning in the simulator to see how viewDidUnload and viewDidLoad are called.

Kendall Helmstetter Gelner