UIViewController 在 dealloc 时不释放子视图(使用 ARC)
我对 UIViewController 有一个看起来很奇怪(非?)的问题。看起来控制器在释放时没有释放其子视图。我将 NSLog 消息放置在所有子视图的 dealloc 方法以及视图控制器中。视图控制器的 dealloc 被调用,但子视图的则没有。但是,如果我随后将该视图控制器的另一个实例推送到导航堆栈,则似乎前一个实例的所有子视图都会被释放(我在控制台中收到一堆 NSLog 消息让我知道)。我已经检查过,并且在呈现视图控制器(正在进行推送的控制器)中没有单独引用自定义视图控制器。
一个小(可能)细节:自定义视图控制器确实接收它存储的块,然后在弹出之前执行。但是,我确实向它发送了 nil,并且得到了相同的行为。另外,呈现的视图控制器在弹出堆栈时会释放,因此没有保留周期。
另外,我确实尝试在自定义视图控制器的 dealloc 方法中显式释放每个视图。同样的行为。
导航控制器有可能会抓住它吗?它似乎没有对我的任何其他视图控制器执行此操作。
我的问题是,这确实代表了内存泄漏(所有这些子视图);虽然泄漏没有叠加,但它仍然是泄漏。
I have what seems like a weird (non?) issue with UIViewController. It appears that the controller is not releasing its subviews when it is dealloc'd. I placed NSLog messages in all of the subview's dealloc method as well as the view controller. The view controller dealloc gets called but the subview's do not. However, if I then push another instance of that view controller on to the navigation stack, it appears that all of the subviews of the previous instance are then released (I get a bunch of NSLog messages in the console letting me know). I've check and I have no separate reference to the custom view controller in the presenting view controller (the one that's doing the pushing).
One small (maybe) detail: The custom view controller does receive a block it stores and then executes before popping. However, I did send nil to it and I get the same behavior. Plus, the presenting view controller does dealloc when popped of the stack, so no retain cycle.
Also, I did try explicitly releasing each view in the dealloc method of the custom view controller. Same behavior.
Is it possible the navigation controller would be holding on to it? It doesn't seem to do this to any of my other view controllers.
My problem is that this does represent a memory leak (of all those subviews); though the leak doesn't stack, it's still a leak.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这很尴尬。我确实在我无意中用作集合类的另一个类(称为 ViewDef)中发现了问题。当我第一次弄清楚一些动画时(几个月前),这是一种快速而肮脏的跟踪子视图的方法。 ViewDef 存储从数据库检索的帧/字体/颜色/等信息,因此在计算动画(方向之间)时也可以方便地存储视图。这些 ViewDef 由我的模型存储并传递,因此当然视图也被保留(并随后被另一个视图控制器替换)。无论如何,我忘记在代码中插入警告以稍后修复此问题。
这个故事的寓意是:如果你打算做一些愚蠢的事情,至少记录下你的愚蠢行为,这样你以后就不必在互联网上传播它。
Ok, this is embarrassing. I did find the problem in another class (called ViewDef) I was inadvertently using as a collection class. It was a quick and dirty way of keeping track of my subviews when I was first figuring out some animations (months ago). ViewDef stored frame/font/color/etc info retrieved from a database, so it was convenient to also store the views when figuring out animations (between orientations). These ViewDefs were being store by my model and passed around, so of course the views were also being retained (and replaced later by another view controller). Anyway, I forgot to insert a warning in my code to fix this later.
Moral of the story: If you plan on doing something stupid, at least document your stupidity so you don't have to broadcast it over the internet later.
您可以尝试在 viewDidUnload 方法中将子视图设置为 nil 也许这会有所帮助
you could try setting the subviews to nil in the viewDidUnload method maybe that will help
要尝试的一件事是确保所有子视图委托都设置为零。
One thing to try is to make sure all your subviews delegates are set to nil.