aneuryzm
aneuryzm

Reputation: 64834

Avoid autorelease of a NSWindow which is opening another NSWindow?

When I open an NSWindow which is autoreleased, everything works correctly. The NSWindow is released by my root class only after I've clicked the OK button.

However, when I open an NSWindow and from the opened NSWindow I open a new NSWindow, I get a bad access error. This happens because the first window is not considered active anymore and it is released by the root class.

How can I avoid this issue ?

Upvotes: 0

Views: 431

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46020

A simple answer: don't do this, you are relying on undefined behaviour and asking for a crash. You should hold a strong reference to the window and only release it when you're done with the window.

In general, you should use an NSWindowController to manage each window that you open. NSWindowController holds a reference to the window and you can easily call the showWindow: and close: methods of NSWindowController to manage window display.

From your question it seems that you are using NSWindow objects as window controllers. This is not how you should be using NSWindow. Use NSWindowController instead, that's what it's for.

Upvotes: 1

Related Questions