为什么在调试模式和运行模式下保留计数不同? [英] Why retain count is diffrent in debug mode and in running mode?

查看:108
本文介绍了为什么在调试模式和运行模式下保留计数不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道ARC和MRC的工作原理.但是在测试以下代码时我感到困惑.我不知道为什么会这样.为什么同一问题的调试模式和运行模式下的保留计数不同?

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));

当我在运行模式下运行该应用程序时,该应用程序在第NSLog(@" 3 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)b));行崩溃,据我了解,因为b是a的weak引用.并在为其分配nil时反对获取释放.但在此之前,如果将前两行的输出显示为下图.这也是正确的.

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.

但是,当应用程序处于调试模式时(意味着我们已经设置了断点并进行调试),则该应用程序没有崩溃,并且每行显示的保留计数为2.如下图所示.

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?

推荐答案

The very first sentence in the documentation for the retainCount method says:

请勿使用此方法.

后来:

通过这种方法获取有用信息的可能性很小

只是不要使用它.

正如trojanfoe在评论中指出的那样,给定代码中使用的CFGetRetainCount带有类似的(可能稍微少一些)免责声明.

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

这篇关于为什么在调试模式和运行模式下保留计数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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