2012-09-11 111 views
2

我解除存檔的NSFileWrapper數據下面的方法通常效果非常好:NSKeyedUnarchiver SIGBUS BUS_ADRALN崩潰

- (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 [email protected]" 
           delegate:self 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alertOFF show]; 

    } 
    return [unarchiver decodeObjectForKey:@"data"]; 
} 

然而,我偶爾會得到一個SIGBUS崩潰的線unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];。我猜這種類型的異常沒有被我的異常處理程序捕獲?我該如何處理這些例外情況以及導致它們的原因?

這裏是Crashlytics崩潰報告:

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 

回答

1

首先,你確定這是整個崩潰報告嗎?應該有更多的細節...

無論如何,這不是一個例外。這是一個信號,通常意味着存儲器訪問錯誤。如果您收到一個SIGBUS,它基本上意味着一個內存錯誤(通常來自您的代碼訪問一個錯誤的指針)。

就你而言,最可能的罪魁禍首是你從[wrapper regularFileContents]收到的數據對象以某種方式損壞。最有可能的是,它返回零。

注爲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.

這將是在那裏我開始。文件數據也可能損壞,並且unarchiver對不良數據感到窒息。

基本上,這是不是一個例外,你可以捕獲並忽略。它表明你的程序中的一個錯誤必須修復。

+2

此外,你應該改變 '返回[取檔decodeObjectForKey:@ 「數據」];'把這個對象在一個臨時的,然後發送一個取檔「finishDecoding」每類文檔信息,然後返回暫時的。 –

+0

@DavidH好一點。我沒有讀過那遠了;-) –