0

I've tried a simple test which results in EXC_BAD_ACCESS error:

#import "AppDelegate.h"

@interface CrashingProxy : NSProxy
@property (strong) id target;
- (instancetype)initWithTarget:(id)targetObj;
@end

@implementation CrashingProxy
@synthesize target;
- (instancetype)initWithTarget:(id)targetObj {
    [self setTarget:targetObj];
    return self;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
    [invocation invokeWithTarget:[self target]];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
    return [[self target] methodSignatureForSelector:selector];
}
@end

@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(20.0, 20.0, 100.0, 32.0)];
    [button setButtonType:NSButtonTypeMomentaryLight];
    [button setTitle:@"Hello"];
    [[[self window] contentView] addSubview:[[CrashingProxy alloc] initWithTarget:button]];
}
@end

Is there something wrong with this test, or is there a fundamental reason why NSView subclasses cannot be used with NSProxy?

Error details:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xc) at [NSView addSubview:]

Boris D.
  • 388
  • 1
  • 5
  • 8
  • Why are you misusing NSProxy in this way? – matt Jun 07 '19 at 23:44
  • 2
    We'd need to see the crash details. However, in general, I've seen places where proxy objects can't be used. Basically, the issue is that the framework accesses instance variables on the passed-in object, rather than necessarily messaging it. – Ken Thomases Jun 08 '19 at 00:27
  • @matt I'm playing with the idea of using NSProxy as a means of bridging Objective-C and another language using its foreign function interface. For this to be useful, I must be able to proxy any Cocoa object. – Boris D. Jun 08 '19 at 01:55
  • @KenThomases Added error details to question. – Boris D. Jun 08 '19 at 02:07

0 Answers0