Why does saveCount remain 1 after [leave the object]?

NSLog(@"first:%u",[object retainCount]); [object release]; NSLog(@"second:%u",[object retainCount]); 

Output:

  first: 1
 second: 1

Why is the object not freed?

+4
source share
5 answers

Quote from the NSObject reference by the keepCount method

This method usually does not matter in memory debugging issues. Because any number of frames, the objects may have saved the object in order to refer to it, while at the same time the abstract pools can hold any number of delayed releases on the object, it is very unlikely that you can use the information from this method.

+13
source

An object may be released, but not when you think it will be. Basically, do not look at saveCount. It may not change until the next runloop or in general, it is an implementation detail. You will get an idea of โ€‹โ€‹when you need to free and when you are not experienced, but until then rely on the clang analyzer.

+4
source

First, retainCount does not give you a number that you can use. It's pointless.

Secondly, the reason why keepCount is 0 is probably because you are trying to work with an object that no longer exists. You are fortunate that your application does not crash because your access to invalid memory. Decreasing the keepCount value before releasing the object is not required, so Apple does not, perhaps.

+1
source

Divide any number by zero and you will find the value "object with zero preservation".

+1
source

I agree with other comments that you do not use saveCount to get a reliable account.

EDIT: Ignore my stupidity below ... :)

However, I noticed that setting the appropriate property for nil ...

 self.object = nil; 

keepCount tends to decrease immediately.

-1
source

Source: https://habr.com/ru/post/1344767/


All Articles