Hi all. I'm just getting into my first garbage collected Cocoa project, and I'm a bit mystified by the behavior I'm seeing. I'm just trying to make a custom about panel. I put this method in my application delegate:

- (IBAction)orderFrontAboutPanel:(id)sender
{
        if (!aboutController)
aboutController = [[AKAboutPanelController alloc] initWithWindowNibName:@"AKSplash"];
        
        [aboutController showWindow:nil];
}

  I put this declaration of aboutController in my app delegate header:

        __weak AKAboutPanelController *aboutController;

And then I implemented AKAboutPanelController like so:

@interface AKAboutPanelController : NSWindowController
{

}
@end

@implementation AKAboutPanelController
- (void)finalize
{
        NSLog(@"AKAboutPanelController finalize");
        [super finalize];
}
@end

My idea — perhaps being too clever for my own good — was that as long as the about window was open, the window would be referenced by the window list, and so the window and its controller would stick around. Choosing the "About" menu item a second time would therefore just call showWindow: on the controller again, ordering the window to the front if it had gotten buried. As soon as the window was closed, I figured it and its controller would be garbage collected, and the weak reference to it in the app delegate would zero out, so then choosing "About" would create a new controller and reload the nib.

  But this is not what happened.

Instead, the about window sticks around for as long as it is the key window (presumably the window is strong-referenced by AppKit or somebody at a lower level than the Kit). As soon as it is not the key window (click on a different window in the app) it gets garbage collected immediately, even though it is still open! Now maybe this is intended behavior, but it sure isn't what I expected; it seemed intuitive to me that an open window, visible to the user, would not be garbage collected. Is there a reason that the window list uses weak references for visible windows?

So. I can think of any number of ways to fix this, of course, but they all seem a little bit gross since they involve either tweaking the garbage collector (CFRetain, for example) or making the reference to the window controller in the app delegate strong, in which it doesn't automatically zero and I have to set up a way for the windowcontroller to tell the app delegate that it's no longer needed. There must be a better pattern; I imagine I'm just not used to thinking in garbage-collection terms yet. So what's the right way to do this?

  Thanks...

Ben Haller
Stick Software

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to