Re: Bizarre Crash Handling Auxiliary Window
Re: Bizarre Crash Handling Auxiliary Window
- Subject: Re: Bizarre Crash Handling Auxiliary Window
- From: Andrew Merenbach <email@hidden>
- Date: Sun, 15 Jun 2003 09:57:31 -0700
There are a couple of small, easily-correctible issues that you may
have in your code: if you have a static shared instance, you may very
well be precluded from releasing it on-the-fly. Static instances are
initialised once and only once, and are not supposed to be reassigned.
Thus for this you may wish to simply have a once-allocated shared
instance that is never released.
The difference between release and autorelease is that release
decreases an object's retain count immediately, whereas autorelease
decreases its retain count at the end of the current run loop, thus
effectively allowing the program to work the rest of the way through
the current method before, say, an object with a retain-count of one is
deallocated.
If you instead wish to call the prefs from mainly a single class, you
may potentially eliminate the need for a shared instance; for example,
your DCMainController (or whatnot) could, in its init: or awakeFromNib:
method, set an instance variable prefsController to [[DCPrefsController
alloc] init], and then in its dealloc: method, release the
prefsController.
Also, if your prefs are in an NSWindowController, you may make things
easier by replacing the line [[self window] makeKeyAndOrderFront:nil]
with [self showWindow:self].
Hope this helps,
Andrew
On Sunday, June 15, 2003, at 08:29 AM,
email@hidden wrote:
Message: 11
Date: Sun, 15 Jun 2003 10:22:27 -0400
Subject: Bizarre Crash Handling Auxiliary Window
From: Christopher Gernon <email@hidden>
To: <email@hidden>
I'm a beginning Cocoa programmer just starting my first "real"
project. I'm
running into a problem handling an auxiliary window (i.e. a window not
intantiated at application launch). For good modular program design, I
want
my preferences window to be loaded from a nib when the user needs it
and
then deallocated when the user is done with it.
Here's the problem: It allocates and displays fine. When the user
clicks the
"Cancel" button or hits Esc, its key equivalent, it exits fine. When
the
user clicks the "OK" button, it exits fine. When the user hits Return
(the
"OK" button's key equivalent) ... "DCTest01 has exited due to signal 10
(SIGBUS)." WTF?!?!?!
Stepping through it in debug, it seems to get through my code fine, no
problems in the "goodbye" function or the dealloc, but then it steps
out
into "NSPopAutoreleasePool" and I get 175 lines of:
[Switching to process 2050 thread 0xb03]
[Switching to process 2050 thread 0x1203]
[Switching to process 2050 thread 0xb03]
[Switching to process 2050 thread 0xb03]
[Switching to process 2050 thread 0x1203]
[Switching to process 2050 thread 0xb03]
... followed by 'Program received signal: "EXC_BAD_ACCESS".' (The trace
stops in "objc_msgSend", so it may be crashing either in that or in
"NSPopAutoreleasePool" - not sure how to read it.)
Any idea what could be going on? My guess is some subtle memory
management
mistake, but it seems really strange that it works fine when the user
CLICKS
the OK button, but not when the key equivalent is used.
Here's my DCPrefsController.m - as you can see, it's pretty much
stubbed-out, boilerplate stuff (I haven't even put any actual
preference-handling code in it yet), which makes me really scratch my
head.
It gets invoked by the main controller object with:
[[DCPrefsController sharedInstance] showWindow:nil];
Any thoughts anyone has would be greatly appreciated! :)
Thanks,
Chris
-----------------------------------------------------------------------
---
#import "DCPrefsController.h"
@implementation DCPrefsController
- (void)awakeFromNib
{
[[self window] makeKeyAndOrderFront:self];
[defaultsUsernameField selectText:self];
}
- (void)dealloc
{
NSLog(@"Deallocating DCPrefsController object\n");
[super dealloc];
}
// --------------- sharedInstance creator/returner
static DCPrefsController *sharedInstance = nil;
+ (DCPrefsController *)sharedInstance
{
if(sharedInstance == nil)
{
sharedInstance = [[DCPrefsController alloc]
initWithWindowNibName:@"Preferences"];
NSLog(@"initialized DCPrefsController object\n");
}
return(sharedInstance);
}
// --------------- exit/release on OK or cancel
- (void)goodbye
{
sharedInstance = nil;
[[self window] performClose:self];
[self autorelease];
// [self release]; // Maybe this would work better than autorelease?
}
// --------------- Cancel/save buttons from window
- (IBAction)prefsCancel:(id)sender
{
[self goodbye];
}
- (IBAction)prefsSave:(id)sender
{
[self goodbye];
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.