简体   繁体   中英

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?

Thanks in advance.

Did you create it with +alloc or +new, or with a method that has the word "copy" in its name? Did you send it a -retain message? 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 ,

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 . 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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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