2012-10-01 58 views
0

我收到以下警告,但我還沒有弄清楚從哪裏開始,有什麼想法?ARC警告 - 「不安全刪除未使用的自動釋放」

NSObject+Be.m:36:3: [rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately 
NSObject+Be.m:35:40: [rewriter] NSInvocation's getReturnValue is not safe to be used with an object with ownership other than __unsafe_unretained 
NSObject+Be.m:36:4: ARC forbids explicit message send of 'autorelease' 

的代碼從這裏走過.... https://github.com/tylerneylon/moriarty/blob/c0d6daf65d86c22b8e5853aef00980f059c92fbc/NSObject%2BBe.m

#import "NSObject+Be.h" 

@interface BeProxy : NSProxy { 
    id target; 
} 

+ (BeProxy *)beProxyForClass:(Class)class; 

- (void)forwardInvocation:(NSInvocation *)anInvocation; 
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector; 

@end 

@implementation BeProxy 

+ (BeProxy *)beProxyForClass:(Class)class { 
    BeProxy *beProxy = [BeProxy alloc]; 
    beProxy->target = [class alloc]; 
    return beProxy; 
} 
// 

// We assume the method called is an init method. The return value 
// may be a new value for self. 
- (void)forwardInvocation:(NSInvocation *)anInvocation { 
    [anInvocation setTarget:target]; 
    [anInvocation invoke]; 
    id object; 
    [anInvocation getReturnValue:(void *)&object]; //HERE 
    [object autorelease];       //HERE 
    [self release]; 
} 

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { 
    return [target methodSignatureForSelector:aSelector]; 
} 

@end 

//  


@implementation NSObject (Be) 

+ (id)be { 
    return [BeProxy beProxyForClass:[self class]]; 
} 

+ (id)beInit { 
    return [[[self class] new] autorelease]; 
} 

- (id)beCopy { 
    return [[self copy] autorelease]; 
} 

@end 
+0

該項目並不意味着在ARC環境中使用。 – Till

回答

4

從文檔:

NSObject的+是該類別旨在幫助內存管理。 具體而言,這使得只能在少量所有權允許的方法之外使用自動發佈的對象。

所以發佈的代碼是幫助手動保留計數內存管理。那麼不要用ARC編譯它。

+0

好奇爲什麼這是被投票 - 是的,措辭可能不完全正確,因爲「手動保留計數」不是什麼「自動釋放」,但總體來說,答案似乎完全符合現場。 – Till

+0

@Till「手動保留計數」或MRC似乎是Objective-C下非ARC內存管理的既定名稱。 –

+0

@NikolaiRuhe Pre-ARC通常寫爲「MRR」或手動保留 - 釋放。 – pkamb