dismissViewControllerAnimated不会释放内存。

3

我有一个UIViewController A,在这一点上我的内存是104 Mb。在A中,我通常像这样打开UIVIewController B:

UIViewController *b = [[UIViewController alloc] initWithLevel:level actualUser:actualUser parentViewController:self];
[self presentViewController:b animated:NO completion:nil];

目前我的内存为132 Mb,当用户触摸按钮时,我会像这样打开UIViewController C:

UIViewController *c = [[UIViewController alloc] initWithBlackboard:3];
[self dismissViewControllerAnimated:NO completion:^{
    [parentViewController(i get this property in the init method) presentViewController:c animated:NO completion:nil];
}];

在这里,我的做法是关闭B然后打开C,这样当我关闭C时,它会直接回到A。

此时我的内存是153 Mb,这是错误的,因为B的内存从未被释放。之后,我会像这样关闭以返回到A:

[self.presentingViewController dismissViewControllerAnimated:NO completion:^{}];

我原本有104MB的内存,现在有132MB,所以B从未被释放。窗口的打开和关闭方式是正确的,但内存却是个问题。

我已经单独测试了每个UIViewController,并没有内存问题。问题出现在它们绑定在一起的时候。有什么想法吗?

我也检查了内存泄漏,但没有发现任何问题。


你的代码中可能有一些对 ViewControllerB 的强引用。 - Eric Qian
我将parentViewController变量设置为__weak UIViewController。 - Fernando Santiago
我的意思是 ViewControllerB 没有被释放,而不是呈现 ViewControllerBViewControllerA - Eric Qian
让我再确认一下。 - Fernando Santiago
2个回答

4

这是一个循环引用。

@interface A : UIViewController {

              UIViewController* B;

          }

@end 

@interface B : UIViewController {

               UIViewController* A;

         }

@end

如果您在使用强引用或默认值的ARC时,会导致内存泄漏。

Instruments工具不总是准确的。在一些复杂的程序中,记录保留计数是一种检查释放是否正确的方法。如果您使用ARC来管理内存,则最好避免循环引用。

这个链接是官方的ARC文档,非常详细。 点击此处查看

只是提供一个小建议。祝您一切顺利!


retainCount文档中的第一句话是:“不要使用此方法。” - InfalibleCoinage

0
问题是一个未被我“释放”的id变量被设置为了nil。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接