views:

78

answers:

1

I am currently working through the famous "Cocoa Programming for OSX" by Aaron Hillegaas.

In Chapter 12 he wants me to create an about window using

[BOOL] successful = [NSBundle loadNibNamed:@"About" owner:self];

which, by itself, works perfectly well. However, I am using the garbage collector and since I do not retain a pointer to that about window, it is garbage collected and thus disappears after a second or two. It works perfectly well if garbage collection is disabled.

Is there a way to create a window without holding a pointer to it and without having it eaten by the garbage collector?

+2  A: 

You can retain the window with CFRetain, or use NSGarbageCollector's disableCollectorForPointer:. However, you can easily introduce a memory leak. Make sure whichever action you use to close the window also releases the window.

If the sender passed to the close action inherits from NSView, it will have a window property that you can use to get a pointer to the window.

However, this is not how Cocoa is designed to work. In Chapter 12 of Hillegaas' book, he has this to say:

When sent showWindow: for the first time, the NSWindowController automatically loads the nib file and moves the window on screen and to the front. The nib file is loaded only once. When the user closes the [window], it is moved off screen but is not deallocated. The next time the user asks for the [window], it is simply moved on screen.

If you deallocate the About window, your app will either crash or appear not to respond the second time someone opens it.

Edit: An alternative (but one that doesn't give you practice in loading nibs) is to add the About window an an NSWindowController to the main nib (make sure you uncheck the About window's "Visible At Launch" attribute). This makes a mess of Main.nib, but can be done entirely in Interface Builder. Connect:

  • the About controller's window outlet to the About window
  • the About controller's showWindow: action to the About menu item
  • if you've your own close button in the About window, connect it to the window's performClose: action.

As for how advisable this course is, Apple has this to say:

A very simple application might be able to store all of its user interface components in a single nib file, but for most applications, it is better to distribute components across multiple nib files. Creating smaller nib files lets you load only those portions of your interface that you need immediately. Smaller nib files results in better performance for your application. They also make it easier to debug any problems you might encounter, since there are fewer places to look for problems.

outis
Of course, how you're going to later release the window if you don't hold a pointer to it is an interesting question. (Not saying you're wrong — I just think the original question may be going down the wrong path.)
Chuck
That's my worry, too.
outis
what's the right way to do it, then? Subclass NSWindowController for a simple About Window without any interactivity?
BastiBechtold
@Paperflyer: just follow chapter 12.
outis
Actually, if this window is supposed to be around for the whole life of the application, there's no reason ever to release it.
Chuck
Thank you guys, I overlooked something in the book (as usual: PEBKAC). But you helped me find my error and get a better understanding of things!
BastiBechtold