简体   繁体   English

保留计数为3的对象

[英]Object with a retain count of 3

I have a UIView which retain count is three, and I just wanted to know how I can release it so that the retain count is 0? 我有一个UIView的保留计数为3,我只是想知道如何释放它,以使保留计数为0?

Thanks in advance. 提前致谢。

Did you create it with +alloc or +new, or with a method that has the word "copy" in its name? 您是使用+ alloc或+ new,还是使用名称中带有单词“ copy”的方法创建的? Did you send it a -retain message? 您是否发送了-retain消息? If not, you don't own it and must not release it. 如果没有,则说明您不拥有它,也不能释放它。 And stop looking at the retain count; 并停止查看保留计数; doing so only serves to over-complicate what is actually a very simple set of rules for memory management. 这样做只会使实际上非常简单的一组内存管理规则变得过于复杂。

Never use retain counts to debug. 切勿使用保留计数进行调试。 The frameworks do some crazy stuff behind the scenes and can frequently cause your retain counts to be very different from what you think they should be. 这些框架在幕后做了一些疯狂的事情,并且经常会导致您的保留人数与您认为应该的数量大不相同。 Release whenever you should and don't worry with it beyond that. 只要您应该释放,就不要担心。

As stated in the official documentation for -retainCount , -retainCount官方文档-retainCount

Important: This method is typically of no value in debugging memory management issues. 重要:在调试内存管理问题时,此方法通常没有任何价值。 Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method. 由于可能有许多框架对象保留了一个对象以保留对其的引用,而同时自动释放池可能会在一个对象上保留任何数量的延迟发布,因此从此获取有用信息的可能性很小方法。

Don't rely on -retainCount . 不要依赖-retainCount Other objects may be retaining your object without you knowing it, and autoreleased objects might give you a wrong impression of the actual retain count. 其他对象可能会在您不知道的情况下保留您的对象,而自动释放的对象可能会给您对实际保留计数的错误印象。

[object release];

but the retain count is probably 3 because the view is currently in use. 但保留计数可能为3,因为该视图当前正在使用中。 You only need to release it once. 您只需要释放一次。 Whatever else is using it (like the parent view or an array) will release it when it is no longer needed. 无论使用什么(例如父视图或数组),都将在不再需要它时将其释放。 Do you perhaps need to remove it from the parent view? 您可能需要将其从父视图中删除吗?

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

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