2013-10-16 41 views
2

我在我的應用程序中使用ARC,並得到一個新的崩潰與這個原因:雙免費在ARC

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

弄明白,我啓用了殭屍的對象,其原因:

*** -[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。如何在下一步更正這個錯誤?

+0

打印每個對象的地址並創建它,並且您將知道(從錯誤消息)哪一個導致問題。 – Floris

+0

謝謝@弗洛里斯。 – Carina

回答

1

__unsafe__unretained NSString *result; ARC會做與它無關。

+0

It works.Thank you,@chroww。 – Carina

3

result在調用傳回時未被保留。 嘗試

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

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

它工作,謝謝。 – Carina

+0

這個工作怎麼樣? CFRetain不能用於弧... – ngb

+0

@ngb false。 ARC中的CFRetain'完全沒問題。 –