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?
You can retain the window with
CFRetain, or useNSGarbageCollector‘sdisableCollectorForPointer:. However, you can easily introduce a memory leak. Make sure whichever action you use to close the window also releases the window.If the
senderpassed to the close action inherits fromNSView, it will have awindowproperty 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:
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:
showWindow:action to the About menu itemperformClose:action.As for how advisable this course is, Apple has this to say: