ios5中的视图和内存管理

发布于 2024-12-29 07:47:40 字数 188 浏览 3 评论 0原文

我只是想知道我当前采用的方法是否存在内存泄漏:

我正在向视图添加多个子视图,并使用保留关键字将每个子视图定义为属性。当调用函数 viewDidUnload 时,我是否需要将指向这些子视图的指针设置为 nil,或者这是自动完成的,因为我使用的是 ARC。我现在在 viewDidUnload 函数中没有执行任何操作。

感谢您的澄清!

I am just wondering if there is any memory leaks in the approach i am currently having:

I am adding multiple subviews to a view and have define each of those subviews as property with the retain keyword. When the function viewDidUnload is called do i need to set the pointer to those subviews to nil or this is done automatically since i am using ARC. I don't do anything right now within the viewDidUnload function.

Thanks for the clarification!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-01-05 07:47:41

通过在 viewDidUnload 中不执行任何操作,您可以保留这些子视图,但不会泄漏它们。

泄漏的对象仍在内存中,但您无法访问它(因为没有指针指向它)。这些子视图不会泄漏,因为您仍然拥有带有指向子视图的指针的视图控制器对象。当视图控制器被释放时,它将释放子视图,然后它们将被释放,而不是泄漏。 (如果视图控制器本身泄漏,那么子视图也会泄漏。)

但是,您应该在 viewDidUnload 中释放子视图,方法是将属性或实例变量设置为 <代码>零。为什么?当内存不足时,系统会卸载您的视图,并向您发送viewDidUnload。如果您在 viewDidUnload 中不执行任何操作,那么当您不需要这些子视图对象时,您就会保留这些对象,从而占用内存。

By doing nothing in viewDidUnload, you are keeping those subviews around, but you aren't leaking them.

A leaked object is still in memory but you have no way to reach it (because there are no pointers left pointing at it). These subviews aren't leaked, because you still have your view controller object with pointers to the subviews. When the view controller is deallocated, it will release the subviews, and then they will be deallocated, not leaked. (If the view controller itself becomes leaked, then the subviews are also leaked.)

However, you should release the subviews in viewDidUnload, by setting the properties or instance variables to nil. Why? The system unloads your view, and sends you viewDidUnload, when it is low on memory. If you do nothing in viewDidUnload, then you're keeping those subview objects around, taking up memory, when you don't need them to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文