Re: Debugging an ignored exception
Re: Debugging an ignored exception
- Subject: Re: Debugging an ignored exception
- From: Scott Morrison <email@hidden>
- Date: Sun, 26 Nov 2006 18:14:05 -0500
You may not want to do this in a shipping app but during beta testing
the following code may be useful,
Rather than just calling the debugger, output a stack trace to the
console.
Make this your exception handler:
- (BOOL)exceptionHandler:(NSExceptionHandler *)sender
shouldLogException:(NSException *)exception
mask:(unsigned int)aMask {
NSLog(@"Error: %@",exception);
fprintf(stderr, "------ STACK TRACE ------\n");
id trace;
if (trace = [[exception userInfo] objectForKey:NSStackTraceKey])
{
NSString* str = [NSString stringWithFormat:@"/usr/bin/atos -p %d %@
| tail -n +4 | c++filt | cat -n", getpid(), trace];
FILE* fp;
if(fp = popen([str UTF8String], "r"))
{
unsigned char resBuf[512];
size_t len;
while( len = fread(resBuf, 1, sizeof(resBuf), fp))
fwrite(resBuf, 1, len, stderr);
pclose(fp);
}
}
fprintf(stderr, "-------------------------\n");
return NO;
}
writes the following to the console (for example)
2006-11-26 18:12:38.336 Mail[10110] Error: *** -[NSCFArray
addObject:]: attempt to insert nil
------ STACK TRACE ------
1 -[NSCFArray addObject:] (in Foundation)
2 -[MailTagsBundle mailTagsTagMessage:] (in MailTags)
(MailTagsBundle.m:985)
3 -[NSApplication sendAction:to:from:] (in AppKit)
4 -[NSMenu performActionForItemAtIndex:] (in AppKit)
5 -[NSCarbonMenuImpl
performActionWithHighlightingForItemAtIndex:] (in AppKit)
6 -[NSMenu performKeyEquivalent:] (in AppKit)
7 -[NSApplication _handleKeyEquivalent:] (in AppKit)
8 -[NSApplication sendEvent:] (in AppKit)
9 0x00038d0e (in Mail)
10 -[NSApplication run] (in AppKit)
11 _NSApplicationMain (in AppKit)
12 0x00094df2 (in Mail)
13 0x00094d19 (in Mail)
14 0x00000001 (in Mail)
-------------------------
This way your testers can grab the stack trace from the console and
email it to you -- helps you track down where the exception is
occuring especially if it is one of those isolated things that
doesn't seem to occur on your development machines.
Scott
I appreciate that the above looks like overkill in comparison to
setting a
breakpoint on -[NSException raise], but consider what happens in
the built
app, not running in the debugger but just running on some user's
machine,
when you get an exception like that. Namely, nothing. The app just
keeps
running, but it is florfed and the user has no way of knowing this
(most
users don't run with the Console open all the time). The above
approach lets
you catch such exceptions and put up a dialog in the exceptionHandler:
routine so the user knows something has gone wrong. Therefore it's
something
that I do in all my apps anyway. m.
________________________________
Scott Morrison <email@hidden>
Creator of Mail Act-On and Mail Tags plug-ins for OS X Mail.app.
<http://www.indev.ca/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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