【问题标题】:Why retainCount returns -1为什么retainCount返回-1
【发布时间】:2012-03-05 14:37:28
【问题描述】:

据我所知,如果对象的保留计数变为 0,则调用它的 dealloc。但我将保留计数设为 -1。什么意思?

我正在使用以下代码-

    Demo *obj1 = [[Demo alloc] init];
    NSString *s = [[NSString alloc] initWithString:@"mithilesh"];
    NSString *s1 = [NSString stringWithString:s];
    [s release];
    object_setInstanceVariable(obj1, [propertyName cString], s1);
    //printing retain count 
    NSLog(@"retain count of name = %i",[obj1.name retainCount]);

输出:

   retain count of name = -1

获取释放时stringWithString:方法返回的字符串?

【问题讨论】:

标签: iphone objective-c memory-management ios4 retaincount


【解决方案1】:

这意味着您需要停止拨打-retainCount。它纯粹是一个调试功能,除非您是从事系统框架或编译器工作的 Apple 工程师,否则您可能没有任何业务可以查看该功能。

也就是说,您在这里看到的是 INT_MAX 被解释为一个有符号的数字。听起来您已经掌握了一个常量字符串,可能是@"mithilesh"。常量字符串是不参与-retain-release 的单例,INT_MAX 中的-retainCount 表明了这一点。但这真的只是猜测,你应该停止关注-retainCount

如果你不相信我,也许你会相信 Bill Bumgarner 说的retainCount is useless

【讨论】:

    猜你喜欢
    • 2011-01-02
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2012-01-25
    • 2017-06-04
    • 2015-01-25
    • 2023-03-27
    相关资源
    最近更新 更多