【问题标题】:CFStringRef seems to hold on to NSString with no strong reference with just __bridge (ARC)CFStringRef 似乎坚持 NSString 没有强引用只是 __bridge (ARC)
【发布时间】:2012-12-22 16:00:54
【问题描述】:

在 ARC 中,__bridge 应该只暗示没有所有权转移的演员表。但是下面的代码 sn -p 不会崩溃:

    int i = 8;
    NSString* str = [[NSString alloc] initWithFormat:@"abc%d",i];
    Employee* e = [Employee newEmployee];
    CFStringRef cfStr = (__bridge CFStringRef)(str);
    str = nil;
    printf("%s\n",CFStringGetCStringPtr(cfStr, kCFStringEncodingMacRoman));

当我将 nil 分配给 str 时,理想情况下应该释放字符串并且 printf 行应该崩溃。

【问题讨论】:

标签: objective-c automatic-ref-counting core-foundation


【解决方案1】:

引用乔希的评论: “内存还没有被重用。如果你打开 malloc scribble 或 guard,你就会崩溃。”

【讨论】:

    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多