在新的ARC内存管理下未取消分配UIViewController [英] UIViewController not being dealloc-ed under the new ARC memory management

查看:79
本文介绍了在新的ARC内存管理下未取消分配UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将UIViewController子类推入导航堆栈;但是,由于它是由navigationController保留的,因此在将其推入堆栈后,我会释放"指向它的指针,以便最终弹出该视图时,该视图控制器将被取消分配.

I am pushing my UIViewController subclass onto the navigation stack; however, since it is being retained by the navigationController, I 'release' my pointer to it after pushing it onto the stack so that when it is eventually popped the viewController will be dealloc-ed.

但是,它不起作用,从不调用viewController的dealloc方法.代码看起来像这样:

However, it's not working, the dealloc method for the viewController is never called. The code looks something like this:

MyViewController *newViewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"foo"];

[self.navigationController pushViewController:newViewController animated:YES];

newViewController = nil;

即使在以下代码中,我的newViewController也不会被取消分配:

Even in the following code, my newViewController is not being dealloc-ed:

MyViewController *newViewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"foo"];
newViewController = nil;

据我了解,在新的自动引用计数(ARC)系统下,一旦没有对象指向对象,则对象将被取消分配.我在正在创建的viewController的dealloc方法中放置了一个NSLog方法,但从未调用过.

From what I understand, under the new Automatic Reference Counting (ARC) system, objects are dealloc-ed once nothing points to it. I put a NSLog method in the dealloc method of the viewController that is being created, but it is never called.

我在这里想念什么?!

感谢阅读我关于堆栈溢出的第一篇文章:)

Thanks for reading my first post on stack overflow :)

** * *

****

对不起.我尝试将第二段代码包装在自动释放池中,然后将其释放.然后,我在没有自动释放池的情况下进行了尝试,并且它也已正确释放.我不知道昨晚发生了什么.

I apologise. I tried the second piece of code wrapped in an autorelease pool and it was deallocated. I then tried it without the autorelease pool and it deallocated properly too. I don't know what happened last night.

(第二次现在它再次停止工作.fffffffuuuuuuuuuuuuu)

(2nd edit: and now its stopped working again. fffffffuuuuuuuuuuuu)

推荐答案

在第一个示例中,如果重新分配了新的视图控制器,那将是很奇怪的,因为您只是将其推到了导航控制器上,因此将其推到了导航控制器上有很强的参考意义.仅当所有强引用都消失时,该对象才会被释放.

In your first example, it would be very odd if the new view controller were deallocated, since you just pushed it onto the navigation controller, and thus the navigation controller has a strong reference to it. The object will only be deallocated when ALL strong references are gone.

在第二个示例中,返回的对象可能仍然具有autorelease实施内部执行的autorelease调用的待处理发行版.因此,在当前的自动释放池耗尽之前,您不会看到它消失.此外,UIStoryboard类可能会将返回的对象作为优化进行缓存.

In your second example, it's possible that the returned object still has pending releases from autorelease calls that were performed internal to the implementation of instantiateInitialViewController. Thus, until the current autorelease pool is drained, you wouldn't see it disappear. Further, it's possible that the UIStoryboard class caches the returned object as an optimization.

简而言之,即使使用ARC,您仍然不能假设从框架方法接收到的对象在完成处理后将立即释放,因为您不能保证该方法的实现中未使用自动释放.

In short, even with ARC you still cannot assume that objects you receive from framework methods will be deallocated immediately when you're done with them, because you cannot guarantee that autorelease was not used in that method's implementation.

这篇关于在新的ARC内存管理下未取消分配UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆