0

I have a class BrowserWindowController that extends NSWindowController. My app delegate has a BrowserWindowController that it allocates, initializes, and points an instance variable at when the app is launched. Then, it displays its window. The goal is to have a window from a NIB show up.

However, the code I am using ends up allocating TWO BrowserWindowControllers and initializes both. I have used the debugger to track down when BWC's initWithWindow method is called:

browser = [[BrowserWindowController alloc] initWithWindowNibName:@"BrowserWindow"]; //this calls initWithWindow as expected
[browser showWindow:nil]; //this allocates ANOTHER BWC and calls initWithWindow on it!

showWindow is making a new BrowserWindowController. I don't know what points to the new object it makes. It's a huge problem for me. Any way to get around this or make the window show up using a different method? Or could I at least get a pointer to the controller that showWindow creates for whatever reason?

sudo
  • 5,604
  • 5
  • 40
  • 78
  • You know, when I call showWindow, it goes through a lot of built-in nib loading method calls. Maybe somewhere in my xib file I have to change something? – sudo Oct 09 '13 at 00:28
  • So I tested in Instruments, and it says that the extra BrowserWindowController allocation's responsible library is AppKit. – sudo Oct 09 '13 at 00:47
  • And one more thing why are you using nil here inspite of using self?? – Hussain Shabbir Oct 09 '13 at 00:52
  • @hussainShabbir You mean in the showWindow parameter? I thought it didn't matter. Anyway, I tried putting self in, but it didn't fix the problem. My app delegate doesn't really have any connection to the window, just that I want the delegate to make a window controller and open that window when the app starts. – sudo Oct 09 '13 at 01:45
  • Do you happen to have the application delegate as a xib top-level object (a blue cube) in more than one nib file? –  Oct 09 '13 at 02:03
  • @Bavarious No, but I also do not have any of those dark blue cubes in the xib, just a light-blue "File's Owner" one with the class set to BrowserWindowController. – sudo Oct 09 '13 at 03:00
  • I tried putting in an object ("dark blue cube") in the xib and setting the class to BrowserWindowController then setting File's Owner to NSApplication and removing all the outlets/actions going to it, and everything is still the same when I run. – sudo Oct 09 '13 at 03:05
  • Oh, you shouldn’t add another object of type `BrowserWindowController` because the nib loading mechanism would create another instance for it. In your code that allocates `BrowserWindowController`, add `NSLog(@"self is %@", self);` and see if there are different objects creating the window controller. –  Oct 09 '13 at 03:49
  • Also, set a symbolic breakpoint in `-[BrowserWindowController initWithWindowNibName:]` and set `bt` as its debugger command action. This should tell you which places are initialising the window controller with a nib file. –  Oct 09 '13 at 04:02
  • I've been using breakpoints and checking the stack trace (essentially the same thing as bt) to determine what is calling the initWithWindowNibName. showWindow calls some Apple code that calls it. I removed the object. If I also set the file's owner to just NSApplication instead of my controller, the problem does not occur, but then my controller is useless since it doesn't have the outlets and actions, of course. But what this means is that the xib probably has something to do with it. – sudo Oct 09 '13 at 04:11
  • Sorry, meant to say that I put the breakpoint in "initWithWindow". initWithWindow is the one getting called twice, not initWithWindowNibName. I put breakpoints in both anyway. The only one that gets initWithWindowNibName called upon it is the one that I call initWithWindowNibName on explicitly. – sudo Oct 09 '13 at 04:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38839/discussion-between-bavarious-and-user1884363) –  Oct 09 '13 at 04:28

2 Answers2

2

Did you check the condition like this and try?

if !(browser)
{
browser = [[BrowserWindowController alloc] initWithWindowNibName:@"BrowserWindow"]; //this calls initWithWindow as expected
[browser showWindow:nil];
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • I tried that just now, and the if condition is satisfied. It's definitely unallocated until the first line of my sample code. – sudo Oct 09 '13 at 00:25
0

Worst solution ever. The problem was that I had a property in my controller called "owner" that was an NSString. NSWindowController already has an "owner" property, and I overlooked that. Somehow, that caused the NIB loader to make a second controller with no accessible pointer to it and do some other weird things.

So I renamed it, and it works now. Thank goodness... I was tearing my hair out with this problem.

sudo
  • 5,604
  • 5
  • 40
  • 78