Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to set these variables to nil in viewDidLoad as I have in this code?

- (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];  
}
like image 293
Andrew Johnson Avatar asked Nov 23 '25 09:11

Andrew Johnson


1 Answers

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.

like image 101
Kendall Helmstetter Gelner Avatar answered Nov 27 '25 03:11

Kendall Helmstetter Gelner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!