在iOS中,使用ARC,是否足以将所有ivars和属性设置为nil,并在viewDidUnload中释放上下文,图像,颜色空间? [英] In iOS, using ARC, is it sufficient to set all ivars and properties to nil, and release context, image, color space in viewDidUnload?

查看:80
本文介绍了在iOS中,使用ARC,是否足以将所有ivars和属性设置为nil,并在viewDidUnload中释放上下文,图像,颜色空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用ARC的iOS应用,我们通常会在 viewDidUnload 中发布这些内容吗?

For iOS apps, using ARC, do we typically release these in viewDidUnload?


  1. 将所有实例变量设置为 nil

将所有属性设置为 nil

使用 CGContextRelease 释放任何上下文,CGImage with CGImageRelease ,以及 CGColorSpaceRelease 的颜色空间(释放任何非对象)

Release any context using CGContextRelease, CGImage with CGImageRelease, and color space with CGColorSpaceRelease (releasing any non object)

NSMutableArray NSSet 元素无需特别注意:只需设置对NSMutableArray的引用即可NSSet为nil,每个元素都会自动释放。

no special attention needed for NSMutableArray of NSSet elements: just set the reference to NSMutableArray and NSSet to nil, and each element will be automatically released.

这些会在ARC下处理大部分内存释放吗?还有其他需要发布的项目吗?

Will doing these handle most memory release under ARC? Are there other items that need to be released as well?

推荐答案

很多人误解了 viewDidUnload 。特别是 viewDidLoad 相对应。在大多数情况下,视图控制器将被取消分配,而不会调用 viewDidUnload 出于这个原因,您应该从不考虑在 viewDidUnload 中取消分配 viewDidLoad <中的分配的适当余额/ code>。

A lot of people misunderstand the point of viewDidUnload. In particular, it is not the counterpart to viewDidLoad. In most cases, your view controller will be deallocated without viewDidUnload being called. For this reason, you should never consider a deallocation in viewDidUnload a proper balance for an allocation in viewDidLoad.

viewDidUnload 已被Apple弃用。可能反对使用它的最好的论据是它的标题(我已经包装):

viewDidUnload has been deprecated by Apple. Possibly the best argument against using it is its header (which I've wrapped):

- (void)viewDidUnload NS_DEPRECATED_IOS(3_0,6_0); // Called after the view
    // controller's view is released and set to nil. For example, a memory warning
    // which causes the view to be purged. Not invoked as a result of -dealloc.`

那么什么是 viewDidUnload ?它背后的想法是视图从视图控制器后面卸载。这使您有机会分离任何指针并清除您可以轻松重建的任何信息。您的视图可能会重新加载,此时您需要重建任何缓存。 Apple描述了这个:

So what was viewDidUnload? The idea behind it is that the view is being unloaded out from behind your view controller. This gives you a chance to detach any pointers to it and clear any information you can rebuild easily. Your view will probably be loaded back in, at which point you'll need to rebuild any caches. Apple describes this:


当出现内存不足的情况并且不需要当前视图控制器的视图时,系统可能会选择删除那些记忆中的观点。在视图控制器的视图发布后调用此方法,并且您有机会执行任何最终清理。如果视图控制器存储对视图或其子视图的单独引用,则应使用此方法释放这些引用。您还可以使用此方法删除对您为支持视图而创建的任何对象的引用,但现在视图已不再使用。您不应该使用此方法来发布用户数据或任何其他无法轻松重新创建的信息。

When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup. If your view controller stores separate references to the view or its subviews, you should use this method to release those references. You can also use this method to remove references to any objects that you created to support the view but that are no longer needed now that the view is gone. You should not use this method to release user data or any other information that cannot be easily recreated.

请记住将参考归零现在应该不需要 viewDidUnload 中的对象。如果您按照Apple的建议使用ARC,那么您的视图出口就会将弱引用归为零。它们会在没有你写 viewDidUnload 的情况下被自动填充!

Keep in mind that zeroing out references to objects in viewDidUnload should be unnecessary now. If you're using ARC per Apple's recommendations, your view outlets are zeroing weak references. They'll automatically be nilled without you writing viewDidUnload!

此外,清除缓存信息更适合 didReceiveMemoryWarning ,所以你应该写一下:

Further, clearing out caching information fits better into didReceiveMemoryWarning, so you should probably write it instead:


你可以覆盖这个方法来发布视图控制器使用的任何其他内存。如果这样做,那么此方法的实现必须在某个时刻调用超级实现,以允许视图控制器释放其视图。如果视图控制器保持对视图层次结构中视图的引用,则应该在 viewDidUnload 方法中释放这些引用。

You can override this method to release any additional memory used by your view controller. If you do, your implementation of this method must call the super implementation at some point to allow the view controller to release its view. If your view controller holds references to views in the view hierarchy, you should release those references in the viewDidUnload method instead.

一般来说,人们放入 viewDidUnload 的东西可以在 viewDidDisappear 中更好地处理的dealloc viewDidUnload 唯一剩下的是你的缓存,可以在视图控制器仍处于打开状态时重建而不会丢失数据,需要时视图重新加载后的某个点。 (而且,这应该在 didReceiveMemoryWarning 中处理。)这些缓存应该是懒惰地构建的;当你的应用再次需要它们时,它会悄悄地重建它们。

Generally, the stuff people put in viewDidUnload is better handled in viewDidDisappear or dealloc. The only thing left for viewDidUnload is nilling any of your caches that can be rebuilt without data loss while the view controller is still open, when needed at some point after the view has been reloaded. (And, again, this should be handled in didReceiveMemoryWarning.) These caches should be built lazily; when your app needs them again, it will quietly rebuild them.

那么你应该在 viewDidUnload 中做什么?如果你正在使用ARC:没什么。甚至不写它。 事实上,自撰写此答案以来,Apple已弃用 viewDidUnload

So what should you do in viewDidUnload? If you're using ARC: Nothing. Don't even write it. In fact, in the time since this answer was written Apple has deprecated viewDidUnload.

对于 CGContextRelease CGContext 资源不是Objective-C对象。 (你指出了这一点,但我想为后代重复一遍。)因此,它不能被ARC自动解除分配。您有责任确保将其解除分配并按照旧的手动保留释放(MRR)内存管理方案进行分配。

For CGContextRelease, the CGContext resource is not an Objective-C object. (You pointed this out, but I want to repeat it for posterity.) As such, it can not be automatically deallocated by ARC. You are responsible for making sure it's deallocated and nilled just as with the old Manual Retain Release (MRR) memory management scheme.

如果您将此代码放在<$ c $中c> viewDidUnload ,不保证会被调用。它必须进入 dealloc 。你可以把它放在 viewDidUnload 中,但是......

If you put this code in viewDidUnload, there is not guarantee it will ever be called. It must go in dealloc. You could put it in viewDidUnload as well, but…

所以:


  1. 没必要或没帮助。与视图层次结构相比,您的实例变量微不足道。

  2. 不必要或有帮助。与视图层次结构相比,您的属性微不足道(只要您的属性不是对视图层次结构的强引用)。

  3. 这将导致内存泄漏,如 dealloc 通常会在没有 viewDidUnload 的情况下被调用。

  4. 没必要或没帮助。见第1点& 2。

  1. Not necessary or helpful. Your instance variables are minuscule compared to the view hierarchy.
  2. Not necessary or helpful. Your properties are minuscule compared to the view hierarchy (as long as your properties are not strong references into the view hierarchy).
  3. This will cause a memory leak, as dealloc will usually be called without viewDidUnload.
  4. Not necessary or helpful. See point 1 & 2.

这篇关于在iOS中,使用ARC,是否足以将所有ivars和属性设置为nil,并在viewDidUnload中释放上下文,图像,颜色空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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