问题
I know that how ARC and MRC works. but I am confuse while testing the below code. I don't know why this happen. Why the retain count is different in debug mode and in running mode for the same question?
NSMutableArray *a = [NSMutableArray array];
[a addObject:@"abc"];
NSLog(@" 1 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)a));
__weak NSMutableArray *b = a;
NSLog(@" 2 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)a));
a = nil;
NSLog(@" 3 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)b));
[b addObject:@"xys"];
NSLog(@" 4 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)b));
When I run the app in running mode the app crash on line NSLog(@" 3 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)b));
that I understand as the b is weak
reference of a. and object a get release when assign nil
to it. but before that if shows the out put of first two line as the the below image. Which is also correct.
But When the app is debug mode (Means we have set break point and debug) then the app didn't crash and also show retain count 2 on each line. as the below image.
Does any one having idea why this happen? Why the same code gives two different retain value for different mode?
回答1:
The very first sentence in the documentation for the retainCount
method says:
Do not use this method.
And later:
it is very unlikely that you can get useful information from this method
Just don't use it.
As trojanfoe pointed out in the comment, the CFGetRetainCount that is used in the given code has a similar (maybe somewhat lesser) disclaimer posted to it.
I always understood this as "it may be useful, but the value might not be what you think it should be. Don't draw any conclusions by looking at the absolute values, because we might do every magic we want to do with it, and it's not your business".
来源:https://stackoverflow.com/questions/35172268/why-retain-count-is-diffrent-in-debug-mode-and-in-running-mode