Elliot_HB
Elliot_HB

Reputation: 41

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) when calling a function in Objective-C

im getting the error Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) when trying to call a function in objective-c++ here is the code I'm trying to run:

int main(int argc, char* argv[])
{
    [WindowController init]; //Fatal error on this line

    return 0;
}

and

#include "Cocoa/Cocoa.h"

@interface WindowController : NSObject
{
@private
    NSWindow* Window;

}
-(id)init;
-(void)close;

@end

@implementation WindowController

-(id)init
{
    id obj = [super init];
    if (obj)
    {
        NSRect WindowRect = NSMakeRect(100, 100, 100, 100);

        Window = [[NSWindow alloc] initWithContentRect:WindowRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:NO];
        [Window setTitle:@"New Window"];
        [Window setReleasedWhenClosed:NO];
        [Window setMinSize:NSMakeSize(50, 50)];

        NSView* View = [Window contentView];
        [View setAutoresizesSubviews:YES];
        [View setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
    }
    return self;
}

-(void)close
{
    [Window close];
    [Window dealloc];
    [super dealloc];
}
@end

Upvotes: 0

Views: 387

Answers (1)

bbum
bbum

Reputation: 162722

There are a ton of problems with this code. In fact, I'd recommend tossing it entirely and starting over with a different tutorial or reference guide as a start.

Cocoa apps simply are not built this way.

Instead, start by going into Xcode, creating a new Cooca app project and then inspect how it is constructed.

Also, the Objective-C here isn't valid, either. The WindowController would have to be allocated before it can be initialized, for example. As well, you never make direct calls to dealloc.

Upvotes: 3

Related Questions