I'm trying to create a method to release the objects I haven't released for any reason yet. I add this on my ViewController's dealloc method:
for (id object in [self.view subviews]){
NSLog(@"/n") ;
NSLog(@"%@", object) ;
if([object isKindOfClass:[UIView class]]){
if (malloc_size(object)>0) {
NSLog(@"-> Releasing object with size: %zd", malloc_size(object)) ;
[object release] ;
}
}
}
Unfortunately, the application crashes. What am I doing wrong?
What am I doing wrong?
You're not balancing your retains and releases. You should never need code like that in your example. If you retain something, release it when you're done with it. Anything else is begging for trouble.
In this code specifically, you're releasing all your view's subviews. Did your code retain each and every one of those views? If no, you shouldn't be releasing them. If yes, why? Are you keeping separate references to each one? Your view will retain any views that you add as subviews; you only need to retain them if you have other references to them (and some people choose not to retain even then).