开发者

iPhone - Retain Count - Retain count goes up for no apparent reason

Quick question, hopefully I am just missing something simple. Ok I have one class that holds a pointer to another; MainMenuClass and NormalGameClass. Inside of the MainMenuClass I do the following.

 m_NormalGame = [[NormalGameMode alloc] initWithNibName:@"NormalGameMode" bundle:[NSBundle mainBundle]];
 m_NormalGame.delegate = self;
 [self prese开发者_运维知识库ntModalViewController:m_NormalGame animated:YES];

Now, I first noticed a problem whenever the NormalGameClass' dealloc function was not being called so I did some retainCount calls and for some reason once it makes its way back to the release function in MainMenu, its retain count is 6. Further digging has me very confused. The first line after viewDidLoad in NormalGameClass its [self retainCount] is 4. Anybody have any idea what could be going on here? I only call alloc on NormalGameClass once ever, and yet it is being retained up to 6? Strangely enough never past that. Thanks for any insight.

UPDATE: Was fiddling around with things and found this to be awkward.In the MainMenuClass, here is how I get rid of the NormalGame.

[self dismissModalViewControllerAnimated:NO];
m_NormalGame.delegate = nil;
[m_NormalGame release];

Now, with this setup, the dealloc for NormalGame is never called. However, if I call [m_NormalGame release] immediately after the one posted above, it calls the dealloc for NormalGame ...twice. =/ Paint me confused.


presentModalViewController retains the passed view controller, so you need to release the view controller passed if you do not autorelease it. In this case you would need to release m_NormalGame.

m_NormalGame = [[NormalGameMode alloc] initWithNibName:@"NormalGameMode" bundle:[NSBundle mainBundle]];
m_NormalGame.delegate = self;
[self presentModalViewController:m_NormalGame animated:YES];
**[m_NormalGame release];**


I would imagine that the -dismissModalViewControllerAnimated: call is not releasing the view controller until the dismissal is complete. You do need to balance your initial -alloc/-init of the controller with a -release, but you should not expect the -dealloc method to be called immediately. It may in fact be called during the next iteration of the run loop, if the object was autoreleased.

Are you saying that without two calls to release your dealloc is not called, or is it simply not called immediately?

Also, try not to inspect retain counts as that will only lead to confusion and headaches. Just follow the memory management rules properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜