I unarchive data in a NSFileWrapper with the following method which usually works very well:
- (id)decodeObjectFromWrapperWithPreferredFilename:(NSString *)p {
NSFileWrapper *wrapper = [self.fileWrapper.fileWrappers objectForKey:p];
if (!wrapper) {
NSLog(@"Unexpected error: Couldn't find %@ in file wrapper!", p);
return nil;
}
NSData *data = [wrapper regularFileContents];
NSKeyedUnarchiver *unarchiver;
@try {
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
}
@catch (NSException *exception) {
NSLog(@"exception: %@", exception);
[TestFlight passCheckpoint:@"FILE LOADING EXCEPTION!"];
UIAlertView *alertOFF = [[UIAlertView alloc]
initWithTitle:@"Corrupt"
message:@"There was an error loading a file! Please contact m@meernotes.com"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertOFF show];
}
return [unarchiver decodeObjectForKey:@"data"];
}
However, I occasionally get a SIGBUS crash for the line unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
. I guess this type of exception is not caught by my exception handler? How can I deal with such exceptions and what causes them?
Here is the Crashlytics crash report:
Exception Type:SIGBUSCode:BUS_ADRALN
Thread 0 Crashed
Latest Crash: 11 September 2012 at 06:23
0 Foundation
-[NSKeyedUnarchiver initForReadingWithData:] + 389
1 Meernotes ✭ FRNBDocument.m line 221
-[FRNBDocument decodeObjectFromWrapperWithPreferredFilename:] + 221
2 Meernotes FRNBDocument.m line 155
-[FRNBDocument settings] + 155
3 Meernotes ModelController.m line 497
__39-[ModelController previewLoadDocAtURL:]_block_invoke_0 + 497
4
...
libdispatch.dylib
_dispatch_barrier_sync_f_slow_invoke + 78
5 libdispatch.dylib
_dispatch_main_queue_callback_4CF$VARIANT$up + 196
6 CoreFoundation
__CFRunLoopRun + 1268
7 CoreFoundation
CFRunLoopRunSpecific + 300
8 CoreFoundation
CFRunLoopRunInMode + 104
9 GraphicsServices
GSEventRunModal + 136
10 UIKit
UIApplicationMain + 1080
11 Meernotes main.m line 16
main + 16
First, are you sure that's the entire crash report? There should be some more detail...
Anyway, that is not an exception. It is a signal, and it usually implies a memory access error. If you receive a SIGBUS, it basically means a memory error (usually from your code accessing a bad pointer).
In your case, the most likely culprit is that the data object that you received from [wrapper regularFileContents]
is somehow corrupt. Most likely, it is returning nil.
Note the documentation for regularFileContents
:
Discussion
This method may return nil if the user modifies the file after you call readFromURL:options:error: or initWithURL:options:error: but before NSFileWrapper has read the contents of the file. Use the NSFileWrapperReadingImmediate reading option to reduce the likelihood of that problem.
That would be where I'd start. It's also possible that the file data is corrupt, and the unarchiver is choking on bad data.
Basically, this is not an exception you can just catch and ignore. It indicates a bug in your program that must be fixed.