Releasing object in viewDidUnload?
In my app im calling presentModelViewController to present a new controller. Everytime this action is triggered memory is allocated. But somehow its not releases properly because at some point my app is using too much memory and it craches.
Probably this is because im not releasing the pr开发者_如何学运维operty objects not correctly ( or not at all ) Is the undermentioned the right way? The dealloc is probably, but what about the viewDidUnload?
- (void)viewDidUnload {
[_sushiTypes release];
_sushiTypes = nil;
}
- (void)dealloc {
[_sushiTypes release];
_sushiTypes = nil;
[super dealloc];
}
Don't forget the call to super in viewDidUnload. You should also access your instance variable through its setter to set it to nil in viewDidUnload. In dealloc, just release the instance variable directly.
- (void)viewDidUnload {
[super viewDidUnload];
NSLog(@"viewDidUnload being called");
self.sushiTypes = nil;
}
- (void)dealloc {
[_sushiTypes release];
NSLog(@"dealloc being called");
[super dealloc];
}
I don't think this is the source of your memory problems though. Does your modal view controller have any other instance variables or IBOutlets?
To help debug further, try using Instruments. From Xcode, go Product > Profile, then choose the Allocations template when Instruments opens. Then open and close your modal view controller multiple times, and inspect your leaks and allocations. If Allocations are growing each time you present/dismiss, then try clicking the 'Mark Heap' button before and after each present/dismiss. You can then inspect the objects that are being allocated and not deallocated with each cycle.
I think I found the problem. You should NEVER assign nil value in the dealloc function. The usual practice while releasing is going to viewDidUnload assign the nil value with the setter and then properly release the property in the dealloc method. Like this:
- (void)viewDidUnload {
self._sushiTypes = nil;
[super viewDidUnload];
}
- (void)dealloc {
[_sushiTypes release];
[super dealloc];
}
Remember to call the super in viewDidUnload like Matty said. You can refer to this SO question for more insight about this process. First set to nil and then release.
精彩评论