Objective-C:为什么我的保留计数不是1?

Tal*_*ode 0 cocoa memory-management objective-c

我有这个非常简单的程序,我只是创建一个对象并查看保留计数.

#import <Foundation/Foundation.h>
#import "GeometryCalculator.h"

int main (int argc, const char * argv[]) {
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

     GeometryCalculator *calculator = [[GeometryCalculator alloc] init];
     NSLog(@"Counter: %d", [calculator retainCount]);

    [calculator release];

        [pool drain];
        return 0;  
}
Run Code Online (Sandbox Code Playgroud)

我希望我的retainCount为1,但它是16863520.Class GeometryCalculator完全是空的.没有方法,没有实例变量.

Jen*_*ton 7

您正在使用垃圾收集进行测试.retainCount在垃圾收集下未定义结果,但实际上,它返回对象的指针值,因为这是最快的未定义事件(在本例中0x1015120).

(Trivia:你也在32位进程中进行测试.如果它是一个64位进程,你会得到指针的高位字,因为Peter引用的类型截断,这将是一个较低的值.)