iPhone - 保留计数 - 保留计数无明显原因增加

发布于 2024-09-16 11:09:35 字数 917 浏览 2 评论 0原文

快速提问,希望我只是错过了一些简单的事情。好吧,我有一个类持有指向另一个类的指针; MainMenuClass 和 NormalGameClass。在 MainMenuClass 内部,我执行以下操作。

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

现在,每当没有调用 NormalGameClass 的 dealloc 函数时,我首先注意到一个问题,因此我执行了一些保留计数调用,并且由于某种原因,一旦它返回到 MainMenu 中的释放函数,其保留计数为 6。进一步挖掘让我很困惑。 NormalGameClass 中 viewDidLoad 之后的第一行其 [self keepCount] 是 4。有人知道这里会发生什么吗?我只对 NormalGameClass 调用一次 alloc,但它最多保留 6 次?奇怪的是从来没有过去。感谢您的任何见解。

更新:正在摆弄东西,发现这很尴尬。在 MainMenuClass 中,以下是我摆脱 NormalGame 的方法。

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

现在,通过此设置,NormalGame 的 dealloc 永远不会被调用。但是,如果我在上面发布的之后立即调用 [m_NormalGame release],它会调用 NormalGame 的 dealloc ...两次。 =/ 让我感到困惑。

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 presentModalViewController: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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛刀软妹 2024-09-23 11:09:35

presentModalViewController 保留了传递的视图控制器,因此如果不自动释放它,则需要释放传递的视图控制器。在这种情况下,您需要释放 m_NormalGame。

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

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];**
时间海 2024-09-23 11:09:35

我想 -dismissModalViewControllerAnimated: 调用在解除完成之前不会释放视图控制器。您确实需要平衡控制器的初始 -alloc/-init 与 -release,但您不应期望立即调用 -dealloc 方法。如果对象是自动释放的,它实际上可能会在运行循环的下一次迭代期间被调用。

您是说,如果没有两次调用来释放您的dealloc,则不会被调用,或者只是没有立即调用?

另外,尽量不要检查保留计数,因为这只会导致混乱和头痛。只需正确遵循内存管理规则即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文