2

我正在使用 UIDocument 加载文件。我现在已经损坏了一个文件,以查看发生了什么以及我的应用程序的行为方式。它会因 EXC_BAD_ACCESS 而崩溃,我现在想知道应该如何处理这种情况。我只是希望文件永远不会损坏吗?在 UIDocument 之前的日子里,我对 NSEXCEPTION 使用了 @try 和 @catch,但这不适用于 EXC_BAD_ACCESS。这是我会使用 NSZOMBIE 的情况吗?据我了解其他帖子,NSZombie 仅用于调试目的,而不是您应该始终依赖的东西。如果我破坏了我的数据,这是抛出 EXC_BAD_ACCESS 的代码行:

 -(BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError {

if (!_books) {
        _books = [[NSMutableArray alloc] init];
    }

        self.books = [NSKeyedUnarchiver unarchiveObjectWithData:contents]; // THIS WILL CRASH IF CONTENTS GOT CORRUPTED

        if ([_delegate respondsToSelector:@selector(libraryDocumentUpdated:)]) {
            [_delegate libraryDocumentUpdated:self];
        }

        return YES;
    }

感谢您的任何建议。

4

2 回答 2

1

An EXEC_BAD_ACCESS is not an exception that you catch, its telling you that you are accessing an invalid memory address, resulting in a crash. NSZombies is just a way of keeping all the objects that should have been deallocated "alive" (therefore not freeing the memory they occupy, which is not what you want in a release build obviously) so as to tell you which "deallocated" you are messaging. You need to work out why you are getting the EXEC_BAD_ACCESS. Is books a retained property?

于 2011-11-12T19:21:42.003 回答
0

您应该找出崩溃的根本原因并修复它。如果你真的想捕捉 EXC_BAD_ACCESS,你可以。新的 C 库SignalRecovery可以使程序能够从操作系统异常(例如 EXC_BAD_ACCESS)中恢复。它可以在 iOS/macOS/Linux 中使用。示例代码:

signal_try(label) {
    // Add your code need try.
    int* ptr = NULL;
    *ptr = 0;
}
signal_catch(label) {
    // Add your code to process exceptions, or do nothing.
    siginfo_t* info = signal_info();
}
signal_end(label)
// Continue run
于 2019-03-15T09:42:34.697 回答