Re: NSWindowController maddness!
Re: NSWindowController maddness!
- Subject: Re: NSWindowController maddness!
- From: j o a r <email@hidden>
- Date: Tue, 9 Nov 2004 19:48:58 +0100
On 2004-11-09, at 16.21, Todd Freese wrote:
I have doc style application using bindings. I created an
AppController to handle creating an NSWindowController to handle a
non-doc style window, which uses a timer for updating.
Timers are a bit tricky, as they retain their target. If you don't
invalidate the timer, the target will never be deallocated.
When I close this window, the NSWindowController never gets released.
The nib is set to “release on close” and the window is singleton. I
have confirmed that windowWillClose is getting called.
I think you mean that the window in the nib is set to release on close,
and that the window controller is a singleton?
Here is my AppController code:
- (IBAction)showVideoWindow:(id)sender
{
if (!videoWindowController) {
videoWindowController = [[VideoWindowController alloc]
init];
}
[videoWindowController showWindow:self];
}
Note that your next call to showVideoWindow after closing the window
controller will trigger a crash. If the window controller is a
singleton, why not add a class accessor method to the window controller
subclass instead? That way it would be easier to avoid the crash as
well... :)
static id sharedInstance = nil;
+ (id) sharedWindowController
{
if (sharedInstance == nil)
{
sharedInstance = [[self alloc] initWithWindowNibName: @"Video"];
}
return sharedInstance;
}
- (void) windowWillClose:(NSNotificatin *) notification
{
// My window will close, so I need to remove myself
[self autorelease];
// If I'm the shared instance, I also need to remove the reference to
myself
if (sharedInstance == self)
{
sharedInstance = nil;
}
}
And my windowController code:
<snip>
- (void)dealloc
{
[super dealloc];
}
You don't need to implement methods where you only call super.
- (void)windowDidLoad
{
[NSThread detachNewThreadSelector:@selector(startRS422Thread)
toTarget:self withObject:nil]; // Startup timer to window update.
}
I think you need to show what's going on in the "startRS422Thread"
method to allow us to know where things go wrong. At least if my
suggestion at the end of this message doesn't pay off.
- (void)windowWillClose:(NSNotification *)aNotification
{
[rs422Controller killTimer]; //Stop the timer.
[rs422Controller release];
Likewise, I think we need to know what you do in "killTimer".
NSWindowController *theWindowController = [[aNotification
object] delegate];
[[theWindowController retain] autorelease];
}
The code for autoreleasing the NSWindowController in the
windowWillClose method was lifted from a posting.
The retain-autorelease pattern is commonly used if you're returning an
object that you've for example removed from a collection. You want to
ensure that the receiver of the object have a chance to take ownership
of the object before it is deallocated. I'm not sure what the point is
in this context though. In all likelihood it's what's causing your
problem. Your window controller already have retain count +1. When you
call retain it will get +2, and after being autoreleased it's back down
to +1 - so it should not be deallocated. Try my implementation of
windowWillClose above and see if it makes any difference.
j o a r
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden