Here's a standalone test.m
file that I'm using to test the behavior.
To compile: clang test.m -o test.app -fobjc-arc -ObjC -framework Foundation
. Make sure the Xcode command-line tools are installed.
#import <Foundation/Foundation.h>
@protocol Protocol
@optional
- (id)objProxyMethod;
@end
@interface ReturnObject: NSObject
@end
@interface Test : NSObject <Protocol>
@end
@interface Proxy : NSObject <Protocol>
- (id)objProxyMethod;
@end
@implementation ReturnObject
- (void)dealloc {
NSLog(@"ERROR:");
NSLog(@"I'm getting deallocated!");
NSLog(@"This shouldn't happen!");
}
- (NSString *)description {
return @"Blank object!";
}
@end
@implementation Proxy
- (id)objProxyMethod {
NSLog(@"in [Proxy objProxyMethod]!");
return [[ReturnObject alloc] init];
}
@end
@implementation Test
- (void)forwardInvocation:(NSInvocation *)invocation {
NSLog(@"Forwarded invocation!");
Proxy *proxy = [[Proxy alloc] init];
[invocation invokeWithTarget: proxy];
NSUInteger length = [[invocation methodSignature] methodReturnLength];
if (length == 8) {
id result;
[invocation getReturnValue:&result];
}
}
@end
int main () {
Test *test = [[Test alloc] init];
id objResult = [test objProxyMethod];
NSLog(@"objResult = \"%@\"", objResult);
return 0;
}
If I comment out [invocation getReturnValue:&result];
, the returned object isn't dealloc
ated. I don't know if this is a bug, or just me misunderstanding how NSInvocation
works.
The problem is that result
is __strong
by default, so when it goes out of scope, the compiler generates a release
for it. But getReturnValue:
didn't give you ownership of the returned object, so your method shouldn't be releasing it.
You can fix this by changing the declaration of result
:
__unsafe_unretained id result;
This prevents the compiler from generating a release
for result
when result
goes out of scope. If you need to retain it, you can copy it to another, __strong
variable.
You could also add a category to NSInvocation
to handle this for you:
@interface NSInvocation (ObjectReturnValue)
- (id)objectReturnValue;
@end
@implementation NSInvocation (ObjectReturnValue)
- (id)objectReturnValue {
__unsafe_unretained id result;
[self getReturnValue:&result];
return result;
}
@end
...
if (length == 8) {
id result = [invocation objectReturnValue];
}
...
You could also report this as a bug. I would expect the compiler, or at least the static analyzer, to warn you that you're converting a pointer to a strong id
to a void pointer. http://bugreport.apple.com
It was because ARC cannot manage objects which was written as pointers. Only directly assignment.
Wrong:
id result;
[invocation getReturnValue:&result];
Right:
void *pointer;
[invocation getReturnValue:&pointer];
id result = (__bridge id)pointer; //Correct, ARC will retain pointer after assignment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With