简体   繁体   English

在ARC下将NSArray设置为nil

[英]Setting NSArray to nil under ARC

I added some logging to all of my init s and dealloc s to try to understand why my ARC-enabled project was consuming more and more memory as runtime continued. 我向所有initdealloc添加了一些日志记录,以试图了解为什么随着运行时间的继续,启用了ARC的项目消耗越来越多的内存。 I found that one view controller was responsible. 我发现一个视图控制器是负责任的。 It's main duty at initialization is to create an NSMutableArray and fill it with NSNull objects. 初始化的主要职责是创建一个NSMutableArray并用NSNull对象填充它。 During execution, it swaps out these null objects for more meaningful objects, and vice versa - a basic lazy loading setup. 在执行期间,它将这些空对象交换为更有意义的对象,反之亦然-基本的延迟加载设置。

When this view controller is popped, any non-null views that are in the NSMutableArray at that time are not released. 弹出该视图控制器时,不会释放当时NSMutableArray中的任何非空视图。 As a result, the view controller itself is also not released. 结果,视图控制器本身也不会释放。

I can resolve this by either emptying the NSMutableArray or setting it to nil. 我可以通过清空NSMutableArray或将其设置为nil来解决此问题。

What makes this necessary? 是什么使得这有必要? Is it always my responsibility to set an array to nil if I created it, even under ARC? 即使我在ARC下创建数组,将数组设置为nil始终是我的责任吗? Or must something else be retaining one of the objects in the array, and preventing it from automatically deallocating? 还是必须有其他方法来保留数组中的对象之一,并防止其自动取消分配?

Assuming the NSMutableArray is an instance variable, it should be released when the view controller is released. 假设NSMutableArray是实例变量,则应在释放视图控制器时将其释放。 If your controller's dealloc is getting called when the NSMutableArray only has NSNull objects, but it's not getting deallocated when you fill your array with "meaningful" content, that means that you undoubtedly have a strong reference cycle , ie there must be some reference to the view controller, itself, in the contents of the NSMutableArray . 如果在NSMutableArray仅包含NSNull对象时调用了控制器的dealloc ,但是在用“有意义的”内容填充数组时并没有释放它,这意味着您无疑具有很强的引用周期 ,即必须有一些引用NSMutableArray的内容中的视图控制器本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM