开发者

How to retain an window without holding a pointer to it in garbage collected Obj-C?

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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜