使用 IBOutlet/NIB 时的内存管理
我的应用程序即将完成,我一直在使用 Instruments 对其进行分析。我正在检查各种对象的保留计数。
我一直小心地释放所有我调用过 alloc 的对象,并且这些对象似乎没有泄漏 - 所以这很酷。
但是,我有一个视图控制器,其中有一个 UIPickerView 。我通过将其拖到 IB 中的 NIB 上来进行设置,使用 IBOutlet 定义属性,合成它,然后将其全部连接起来。
每次我启动视图时,UIPickerViews 的数量似乎都会增加一个。我假设我不需要自己释放这种东西,因为我已经将它分配给了一个属性(使用非原子,保留)。
我的所有 UI 内容(按钮等)都发生这种情况,而不仅仅是选择器视图。我只是用它作为例子。
有人可以帮我吗?
谢谢!
My app is nearly finished and I have been Profiling it using Instruments. I'm checking out retain counts of various objects.
I have been careful to release any objects which I have called alloc on, and these don't seem to be leaking - so thats cool.
However, I have a view controller which has a UIPickerView in it. I set that up by dragging it onto my NIB in IB, defined the property using IBOutlet, synthesized it, and then hooked it all up.
Every time I launch the view, it seems the number of UIPickerViews increases by one. I was under the assumption that I do not need to release this kind of thing myself, as I had assigned it to a property (using nonatomic, retain).
This is happening to all my UI stuff - buttons etc, not just the picker view. I was just using that as an example.
Can anyone help me out here?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当视图加载时,从 NIB 创建的所有项目都会为您保留,并且您的 IBOutlet 指针将被初始化,指向那些保留的子视图对象。您需要在 viewDidUnload 中释放它们。你在这样做吗?
在 viewDidUnload 中,您应该释放从 NIB 创建的所有对象并将这些指针设置为 nil。您还应该在 dealloc 中释放这些相同的对象。 示例。
When the view is loaded all the items created from the NIB are retained for you and your IBOutlet pointers are initialized pointing to those retained subview objects. You need to release those in viewDidUnload. Are you doing that?
In viewDidUnload you should release all objects created from the NIB and set those pointer to nil. You should also release those same object in dealloc. Example here.