How to fix strange retain count (1 init - 3 retaincount)? + edit: dealloc problem
So my code goes like this:
ArticleControllerController *ac = [[ArticleControllerController alloc] init];
ac.categoryIndex = idx;
NSLog(@"acc retain: %d", [ac retainCount]);
[app.nav pushViewController:ac animated:NO];
NSLog(@"acc retain: %d", [ac retainCount]);
[ac release];
NSLog(@"acc retain: %d", [ac retainCount]);
And I get:
[2649:207] acc retain: 1
[2649:207] acc retain: 3
[2649:207] acc retain: 2
How to resolve this mess? I don't understand what I'm doing wrong and this part sometimes causes application crash due low memory.
Edit: related problem.
So the 开发者_C百科situation is the same as defined above, but the problem is that ArticleControllerController dealloc method never gets called.
More code:
- (void) navigateToNewsCategoryByIndex:(int)idx {
[app.nav popViewControllerAnimated:NO];
currentMode = MODE_ARTICLE;
ArticleControllerController *ac = [[ArticleControllerController alloc] init];
ac.categoryIndex = idx;
[app.nav pushViewController:ac animated:NO];
[ac release];
return ;
}
If this method gets repeated several times ArticleControllerController creates massive amounts of various interface elements, but its dealloc method never releases them (retain count never goes down to zero), so I think here lies the memory-crash problem I am trying to resolve for a couple of days now.
What's up with that? Can I do something more to resolve this?
That looks fine to me. After you've created it 'ac' has a retain count of 1 which is correct. Then you push it to app.nav and in there a further two retains are counted which is fine. It is the responsibility of that class to release whatever it retains. Finally you release the instance you have created and the retain count goes down to 2. But those two counts are not your responsibility, that is app.nav.
This is why you should not really worry about printing out retainCount as it can look weird when you don't know what is going on behind the scenes in another class.
All you need to do is one release for every alloc and you have done that
精彩评论