mKane
mKane

Reputation: 982

NSWindow crashes after close and reopen

I have created an NSWindow

self.storeWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 800, 660) styleMask:NSWindowStyleMaskResizable|NSWindowListOrderedFrontToBack|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:false];
                             [self.storeWindow setDelegate:self];

Which opens fine and closes fine. However if I call if again of even check for nil it crashes with EXC_BAD_ACCESS.

I declare it as a string property in the header

@property (strong,nonatomic) NSWindow *storeWindow;
      if (self.storeWindow.contentView == nil) {
                            self.storeWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 800, 660) styleMask:NSWindowStyleMaskResizable|NSWindowListOrderedFrontToBack|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:false];
                             [self.storeWindow setDelegate:self];
                        }
                        [self.storeWindow setBackgroundColor:[NSColor whiteColor]];
                        self.store = [[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 850, 640)];
                        [self.store loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:json[@"url"]]]];
                        [self.storeWindow.contentView addSubview:self.store];
                        [self.storeWindow makeKeyAndOrderFront:self.window];

Any ideas?

Upvotes: 4

Views: 1183

Answers (1)

red_menace
red_menace

Reputation: 3412

NSWindow’s releasedWhenClosed property can be set to change the default behavior, which is to release a window when it is closed (unless it is owned by a window controller). The property can be set in the Interface Editor’s Attributes Inspector, or programmatically, for example:

[myWindow setReleasedWhenClosed:NO];

Upvotes: 5

Related Questions