Re: Release a NSWindowController after the window is closed
Re: Release a NSWindowController after the window is closed
- Subject: Re: Release a NSWindowController after the window is closed
- From: Roland King <email@hidden>
- Date: Mon, 20 Jun 2011 08:11:35 +0800
How about adding an instance variable to your window controller subclass which keeps a strong reference to itself. Also register as your own delegate, and nil the strong reference in your windowWillClose: (or perhaps windowDidClose:)
Effectively you now have your delegate (yourself) retaining the thing to which it is a delegate, which isn't unusual.
On 20-Jun-2011, at 3:35 AM, Marc Respass wrote:
> Hi Brian,
>
> The technique that I have been using for a long time is to alloc/init the window controller, make the window controller the delegate of the window, and invoke [self autorelease] in windowWillClose:. It's essentially the same thing you are doing with less code.
>
> The thing that I love about this technique is that my window is now completely independent. If it's a window used as a sheet, I may have many different objects using it. Simply closing the window cleans up everything.
>
> As others have pointed out, this technique is incompatible with ARC. There are two ways around that. I would like to know what others think about them. One solution is to create a static NSMutableSet in the window controller that holds all instances of that window controller. In -init, you can do [windowControllerSet__ addObject:self]; and in windowWillClose: you invoke [windowControllerSet__ removeObject:self];. Of course windowControllerSet__ is static so it lives for the life of the app.
>
> Another solution would be that every object that creates an instance of your window controller (though it probably works for any instance of any window controller) also holds an array of those window controllers and it registers to receive the windowWillClose: notification so it can remove it.
>
> As I say above, I like the independence of the window controller being up to clean up after itself. It's also a lot less code.
>
> Hope this helps
> Marc
>
>> Hello.
>>
>> I'm building a Cocoa application and have a question about using
>> window controllers. The idea is that when the user selects New from
>> the File menu, an instance of MyWindowController which is a subclass
>> of NSWindowController is created and a new window from MyWindow.xib is
>> displayed.
>>
>> I'm handling the action in the application delegate. From what I have
>> seen after searching around something like the following could be
>> done. Once the window is displayed I don't have any reason to store a
>> pointer to the window controller anymore and since I allocated it I
>> also have it autoreleased before displaying the window.
>>
>> [[[[MyWindowController alloc] init] autorelease] showWindow:self];
>>
>> Since the window is released soon afterwards the window will briefly
>> display on the screen and then go away. I'm using a solution where I
>> retain the window controller in the -showWindow: method and let it
>> release itself once it gets a windowWillClose notification.
>>
>> - (IBAction)showWindow:(id)sender
>> {
>> void (^windowWillCloseHandler)(NSNotification *) = ^(NSNotification *note) {
>> [[NSNotificationCenter defaultCenter] removeObserver:self
>> name:NSWindowWillCloseNotification object:self.window];
>> [self release];
>> };
>>
>> [self retain];
>> [[NSNotificationCenter defaultCenter]
>> addObserverForName:NSWindowWillCloseNotification object:self.window
>> queue:nil usingBlock:windowWillCloseHandler];
>> [super showWindow:sender];
>> }
>>
>> Is there a better way to do this? I have searched the documentation
>> and have not found anything specific on which practices to use. It
>> sounds like something very basic which it should cover so maybe I'm
>> just searching with the wrong terms.
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden