Re: My try/catch block isn't catching exceptions on 10.6
Re: My try/catch block isn't catching exceptions on 10.6
- Subject: Re: My try/catch block isn't catching exceptions on 10.6
- From: Jeremy Pereira <email@hidden>
- Date: Fri, 27 Nov 2009 12:22:05 +0000
On 26 Nov 2009, at 23:51, Mark Allan wrote:
>
>
> Exception Type: EXC_BAD_ACCESS (SIGBUS)
> Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000010
> Crashed Thread: 3
You can't catch that with an Objective C exception handler. It's a Unix bus error. Probably some pointer has changed under your code.
> // We don't really care if imminentList changes because the dictionary object will always be there even if the contents aren't.
Yes you do. NsMutableDictionary methods are not thread safe. If you have other threads changing imminentList, while this thread is inserting stuff, you'll need locking.
> [self getLock4itemList];
> @try{
> [listToCopyFrom retain];
> int counter = 0;
> for (counter=0; counter < [listToCopyFrom count]; counter++) {
> NSString *theKey = [listToCopyFrom objectAtIndex:counter];
> if(theKey != nil){
> if ( ([myItemList objectForKey:theKey] == nil) && ([imminentList objectForKey:theKey] == nil)) {
> [myItemList setObject:[NSNumber numberWithLongLong:-1] forKey:theKey];
> }
> else{
> //theKey already exists, don't add it again
> }
> }
> }
> [listToCopyFrom removeAllObjects]; // remove all the objects so we don't see them again next time around
> [listToCopyFrom release]; // counteracts the retain at start of this method
> }
> @catch (NSException *e) {
> NSLog(@"Splat! Reason: %@", [e reason]);
> }
> [self releaseLock4itemList];
> }
Looking at the above code, I don't think there is anything in it that could throw an exception that you would want to handle. sending objectToKey: to listToCopyFrom could raise an exception if it has become shorter between the loop test and sending the message, but that's a programmer error. Sending setObject:forKey: could throw an exception if the NSNumber is nil, but that would mean virtual memory exhaustion, what are you going to do if you catch an out of memory condition? So I think your try catch block is superfluous in this instance.
However, if there was an code that could throw an exception you wanted to handle, your block currently leaks an object reference, because the exception would bypass [listToCopyFrom release]. I tend to use the following idiom:
[anObject retain];
@try
{
// do stuff that might throw an Objective C exception
}
@catch (NSException* e)
{
// handle the exception
}
@finally
{
[anObject release];
}
>
> Thanks
> Mark
>
> _______________________________________________
>
> 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
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
_______________________________________________
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