ARC下双重免费

时间:2013-10-16 03:52:29

标签: ios objective-c automatic-ref-counting

我在我的应用程序中使用ARC并因此而出现新的崩溃:

malloc: *** error for object 0x17e9a5d0: double free
*** set a breakpoint in malloc_error_break to debug

为了解决这个问题,我启用了Zombie Objects,原因是:

*** -[CFString release]: message sent to deallocated instance 0x15d183e0

我的代码:

Class myClass = NSClassFromString(classString);
SEL mySelector =  NSSelectorFromString(selectorString);
NSString *arg = @"arg";

NSMethodSignature *sig = [myClass methodSignatureForSelector:mySelector];
NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature:sig];

[myInvocation setTarget: myClass];
[myInvocation setSelector: mySelector];
[myInvocation setArgument:&arg atIndex:2];

NSString *result = nil;
[myInvocation retainArguments];
[myInvocation invoke];
[myInvocation getReturnValue: &result]; 

NSLog(@" Result String : %@ ",result);

出了什么问题?哪个CFString ?? 感谢您的回复。

修改

对象NSString *result造成的。如何在下一步中纠正此错误?

2 个答案:

答案 0 :(得分:3)

调用传回时不保留

result。 尝试

CFStringRef result;
[myInvocation retainArguments];
[myInvocation invoke];
[myInvocation getReturnValue:&result]; 
if (result)
    CFRetain(result);

NSLog(@" Result String : %@ ", (__bridge NSString *)result);

答案 1 :(得分:1)

__unsafe__unretained NSString *result; ARC对此无能为力。