Xcode断点系统如何与内存管理交互?

时间:2019-02-07 03:05:01

标签: objective-c xcode automatic-ref-counting breakpoints lldb

这是一个小实验:

sketch/src/iotc/sdk_plat/iotc.cpp:14:53: fatal error: azure_prov_client/prov_security_factory.h: No such file or directory

运行时,将产生以下内容:

@interface Model : NSObject

@property (copy) NSString *value;

-(instancetype)initWith:(NSString *)value;

@end

@implementation Model

-(instancetype)initWith:(NSString *)value {
    self = [super init];
    self.value = value;
    return self;
}

@end

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

void experiment(Model *m);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");

        Model *ma = [[Model alloc] initWith:[[NSUUID UUID] UUIDString]];

        experiment(ma);
        NSLog(@"yyy %@", [ma value]);
    }
    return 0;
}

void experiment(ModelA *m) {
    NSString *testValue = nil;
    testValue = [m value];
    NSLog(@"xxx %@", testValue);
}

但是假设我做这行:

Hello, World!
xxx 6005A7B0-F71C-4755-B1BF-792D6296B716
yyy 6005A7B0-F71C-4755-B1BF-792D6296B716
Program ended with exit code: 0

断点的一部分:

enter image description here

这将改变一切:

testValue = [m value];

然后崩溃。我知道发生了什么—一旦退出函数作用域,字符串就被释放,而第二次Hello, World! (__NSCFString *) $0 = 0x000000010071e220 @"1C0DCB39-BFBB-4E67-A041-E6B58615BDFD" xxx 1C0DCB39-BFBB-4E67-A041-E6B58615BDFD yyy 1C0DCB39-BFBB-4E67-A041-E6B58615BDFD *** -[CFString release]: message sent to deallocated instance 0x10071e220 对象被销毁时,字符串被释放,这是一个过度释放。但是,为什么断点(或更确切地说,断点内的lldb表达式)不能正确处理引用计数?

0 个答案:

没有答案