It seems that any NSInvocation with a return value that's a struct gives a garbage return value. Furthermore, as demonstrated by the little test below, it seems to even mess up its target object when it is invoked. The logging of [vertex location] segfaults after the invocation, but works before. If I comment that out, thus allowing the code to log the return value, that prints incorrect x and y values.
Any idea what's going on here or at least where I could start debugging this?
Thank you!
@interface IRVertex : NSObject
{
NSPoint location;
}
@property (nonatomic,assign) NSPoint location;
@end
@implementation IRVertex
@synthesize location;
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
IRVertex* vertex = [[IRVertex new] autorelease];
[vertex setLocation:NSMakePoint(4,2)];
NSLog(@"Value before invocation:");
NSLog(@"%@",NSStringFromPoint([vertex location]));
SEL sel = @selector(location);
id sig = [vertex methodSignatureForSelector:sel];
id inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setSelector:sel];
[inv invokeWithTarget:vertex];
NSUInteger returnLength=[sig methodReturnLength];
void *returnValue=__builtin_alloca(returnLength);
[inv getReturnValue:returnValue];
NSLog(@"Value after invocation:");
NSLog(@"%@",NSStringFromPoint([vertex location]));
NSLog(@"Invocation return value:");
NSLog(@"%@",NSStringFromPoint(*(NSPoint*)returnValue));
[pool drain];
return NSApplicationMain(argc, (const char **) argv);
}