[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Question about message forwarding
From: |
Issac Trotts |
Subject: |
Question about message forwarding |
Date: |
Fri, 14 Apr 2006 22:15:46 -0700 |
Here is a test I wrote to track down a problem with message forwarding
in another program I'm writing.
// fowardingTest.m
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface Delegate: NSObject
{
}
-(void)foo;
-(void)bar:(int)key;
-(void)baz:(NSNumber*)key;
@end
@implementation Delegate
-(void)foo
{
printf("Delegate %p foo\n",self);
}
-(void)bar:(int)key
{
printf("Delegate %p bar: %i\n",self,key);
}
-(void)baz:(NSNumber*)key
{
printf("Delegate %p baz: %i\n",self,[key intValue]);
}
@end
@interface Forwarder: NSObject
{
Delegate* delegate;
}
-(id)initWithDelegate:(Delegate*)d;
@end
@implementation Forwarder
-(id)initWithDelegate:(Delegate*)d
{
if((self=[super init])==nil) { return nil ; }
delegate = d;
return self;
}
-(void)forwardInvocation:(NSInvocation*)invo
{
[invo invokeWithTarget:delegate];
}
-(NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [delegate methodSignatureForSelector:sel];
}
@end
int main()
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
Delegate* d = [Delegate new];
Forwarder* f = [[Forwarder alloc] initWithDelegate:d];
[f foo];
[f bar:1234];
[f baz:[NSNumber numberWithInt:1234]];
[pool release];
return 0;
}
Here is the output:
Delegate 0x8063bf0 foo
Delegate 0x8063bf0 bar: -1078528424
Segmentation fault
So I would like to know why bar does not show 1234. Looking at a
stack trace, baz does get called, but I think the `key' argument is
corrupted, hence the crash. So, why are the args getting trashed?
Thanks,
Issac
- Question about message forwarding,
Issac Trotts <=