Skip to content

Instantly share code, notes, and snippets.

@wbyoung
Created February 29, 2012 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbyoung/1941795 to your computer and use it in GitHub Desktop.
Save wbyoung/1941795 to your computer and use it in GitHub Desktop.
NSInvocation va_arg crash explanation
// clang -o test test.m -framework Foundation
#import <Foundation/Foundation.h>
@interface Forwarder : NSObject {
@package;
id destination;
}
@end
@implementation Forwarder
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
return [destination methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
NSLog(@"forwarding %tu arguments with frame length of %tu", [[anInvocation methodSignature] numberOfArguments], [[anInvocation methodSignature] frameLength]);
[anInvocation setTarget:destination];
[anInvocation invoke];
}
@end
@interface Destination : NSObject
- (void)logWithFormat:(id)format, ...;
@end
@implementation Destination
- (void)logWithFormat:(id)format, ... {
va_list ap;
va_start(ap, format);
NSString *string = [[[NSString alloc] initWithFormat:format arguments:ap] autorelease];
va_end(ap);
NSLog(@"%@", string);
}
@end
int main() {
// it seems that the va arg stuff works through NSInvocation up until a point
// only by luck that the invocation thinks the frame is a bit bigger than it really is.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Destination *destination = [[Destination alloc] init];
Forwarder *forwarder = [Forwarder alloc];
forwarder->destination = destination;
[(id)forwarder logWithFormat:@"3 args"];
[(id)forwarder logWithFormat:@"%@", @"4 args"];
[(id)forwarder logWithFormat:@"%@%@%@", @"", @"", @"5 args"];
[(id)forwarder logWithFormat:@"%@%@%@%@", @"", @"", @"", @"6 args"]; // crashes here
[destination release];
[forwarder release];
[pool release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment