views:

33

answers:

1

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 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?

+2  A: 

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

willcodejavaforfood
thank you very much! If it's not too much to ask, could you take a look at my edit - I think I have a huge problem related to completely releasing my controller (I guess its retain count never goes down to zero and very badly messes up with application memory).
sniurkst
You really should not ask a new question in an old one, especially after accepting an answer :) Rollback your question and post a new with with this OTHER problem and I'll be happy to take a look
willcodejavaforfood
Thanks for clearing that up :) Here it is: http://stackoverflow.com/questions/3905702
sniurkst