1

Question...

I’ve previously used this code from my MainMenu.xib to manage windows:

--

if([olt_MainMenu_WINDOW isVisible]){
    [olt_MainMenu_WINDOW setReleasedWhenClosed:NO];
    [olt_MainMenu_WINDOW close];
}

...and then

if(olt_DoSomething_WINDOW == nil)
    if(![[NSBundle mainBundle] loadNibNamed:@"DoSomething" owner:self topLevelObjects:nil])
        NSBeep();
if(olt_DoSomething_WINDOW != nil)
    [olt_DoSomething_WINDOW makeKeyAndOrderFront:nil];

--

...then go back to my window in my MainMenu.xib:

--

if([olt_DoSomething_WINDOW isVisible]){
    [olt_DoSomething_WINDOW setReleasedWhenClosed:NO];
    [olt_DoSomething_WINDOW close];
}

...and then

if(olt_MainMenu_WINDOW == nil)
    if(![[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:self topLevelObjects:nil])
        NSBeep();
if(olt_MainMenu_WINDOW != nil)
    [olt_MainMenu_WINDOW makeKeyAndOrderFront:nil];

--

but if I try to makeKeyAndOrderFront the DoSomething window a second time, it barfs with exc_bad_access (code=1 ...

I can makeKeyAndOrderFront the MainMenu more than once but not any others. What am I missing?

Yes, 'Release When Closed’ is not selected in the Attributes inspector for the window. This is the first time I've used this with ARC, could it be related to that?

  • 2
    Run the Analyzer. Any warnings? Fix them. Then run with zombies enabled. Any zombie errors? Fix them, or edit your question to include the errors. (See [this](https://developer.apple.com/library/ios/recipes/xcode_help-scheme_editor/Articles/SchemeDiagnostics.html) or [this](https://developer.apple.com/library/ios/recipes/Instruments_help_articles/FindingMessagesSenttoDeallocatedObjects/FindingMessagesSenttoDeallocatedObjects.html) for ways to enable zombies.) – rob mayoff Apr 27 '15 at 03:04
  • 1
    I think this SO question might help you: http://stackoverflow.com/questions/1221516/does-an-iboutlet-needs-to-be-a-property-synthesized – Cristik Apr 27 '15 at 20:09
  • Analyzer returns no warnings, but I get a hit with zombie: '2015-04-27 14:06:22.730 six[4660:97785] *** -[DoSomething windowDidBecomeKey:]: message sent to deallocated instance 0x6080000a5a60 (lldb)' – Henrietta Read Apr 27 '15 at 21:37
  • Henrietta, please take a screenshot of the window's retain / release history in Instruments and post it. – Joshua Nozzi Apr 28 '15 at 12:38
  • Where and how are you storing the DoSomething window (controller) ptr? – Rupert Pupkin Apr 28 '15 at 16:44

1 Answers1

1

Based on the reference for loadNibNamed:owner:topLevelObjects:, I'm going to guess that you have a weak IBOutlet to the NSWindow top level object in the doSomething nib. Since you're not passing something like &myArray and instead using topLevelObjects:nil, it's pretty clear that "it is necessary to keep a strong reference to them by using IBOutlets."

Discussion

Unlike legacy methods, the objects [topLevelObjects-ed.] adhere to the standard cocoa memory management rules; it is necessary to keep a strong reference to them by using IBOutlets or holding a reference to the array to prevent the nib contents from being deallocated.

Outlets to top-level objects should be strong references to demonstrate ownership and prevent deallocation.

You need:

@property (strong) IBOutlet NSWindow *olt_DoSomething_WINDOW;

... or to otherwise keep a strong reference to the window. IBOutlets are generally weak because they are view elements retained by their superview that you do not need the controller retaining, but your NSWindow here has no retaining owner unless you explicitly create one. So it would be a case where you actually want a strong IBOutlet.

Community
  • 1
  • 1
stevesliva
  • 5,351
  • 1
  • 16
  • 39
  • Thank you Steve, Chris and Rob. @property (strong) IBOutlet NSWindow *olt_DoSomething_WINDOW; works. Also, to update the code to work as posted above (a few years ago) I needed to change 'setReleasedWhenClosed' to 'orderOut'. :) – Henrietta Read May 01 '15 at 19:17
  • 1
    BTW, `strong` is implicit when nothing else is specified, but it's nice to be explicit in this case. – stevesliva May 01 '15 at 22:59