OS X上的ARC:调试与发布配置中的不同行为

时间:2014-07-11 13:47:11

标签: ios objective-c macos memory-management automatic-ref-counting

考虑以下应用。当我在调试配置中运行此应用时,textStorage始终在NSLog()非零

但是,当我在发布配置中运行应用时,textStorage始终在NSLog() nil 。这是有道理的,因为textStorage在被分配后未被使用,因此ARC立即释放它(layoutManager必须保持一个弱的后向引用。)

但为什么应用程序在调试配置中表现出不同的行为?什么是ARC做的不同以及为什么?

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSLayoutManager *layoutManager = nil;
    NSTextContainer *textContainer = nil;
    NSTextStorage *textStorage = nil;
    if (YES) {
        textStorage = [[NSTextStorage alloc] initWithString:@"test"];
        textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(100, 100)];
        layoutManager = [[NSLayoutManager alloc] init];
        [layoutManager addTextContainer:textContainer];
        [textStorage addLayoutManager:layoutManager];
    }
    NSLog(@"breakpoint here!");
}

@end

0 个答案:

没有答案