【发布时间】:2011-12-06 22:05:19
【问题描述】:
我有一个简单文本编辑器的 NSDocument 子类(使用 Lion 的新基于文档的应用程序模板,几乎没有自定义),我在将文件内容加载到文本存储时遇到了一个奇怪的错误。
这是我的代码:
- (void)loadTextContentIntoStorage
{
if (!self.textStorage || !textContentToLoad)
return;
...
[self.textStorage beginEditing];
// NSLog(@"storage: %@ length: %lu textContent: %@", self.textStorage, (unsigned long)self.textStorage.length, textContentToLoad);
// [self.textStorage replaceCharactersInRange:NSMakeRange(0, self.textStorage.length) withString:textContentToLoad];
[self.textStorage replaceCharactersInRange:NSMakeRange(0, 0) withString:@"hello world"];
..
[self.textStorage endEditing];
}
错误发生在我:
- 在 Xcode 中运行应用程序(作为调试版本)
- 打开任何文档
- 退出应用(不关闭文档)
- 再次从 Xcode 运行应用程序
它在 -replaceCharactersInRange:withString: 中崩溃,并显示“无法将字符串 0x10004d430 中的字节转换为 _NSCStringEncoding”。
但它只会在应用程序的每 秒 次启动时发生(第三次启动不会崩溃,并且它会自动重新打开它尝试打开时崩溃的文档上一次)。它也只发生在我从 Xcode 运行应用程序时。发布版本从未在发布时崩溃。
我认为这可能是自动保存系统的编码问题,但是当我注释掉该代码并将@“hello world”字符串加载到文本视图中时它甚至崩溃(如上面的代码所示)。同样,注释掉的NSLog() 并没有显示任何奇怪的东西。文本存储有效(从xib文件加载),文本存储长度为0,textContent为正在打开的文件的内容。
--- 编辑---
我了解到这个问题在某种程度上与com.apple.security.app-sandbox 权利有关。如果权利/沙盒启用,那么我的应用不会崩溃。如果权利或应用程序沙盒功能被禁用,那么我的应用程序在每次启动尝试恢复之前打开的文档时都会崩溃。
我只注意到它在从 xcode 内部进行构建/运行时崩溃,因为这是我唯一禁用沙盒的构建配置。
--- /编辑---
有人有什么想法吗?完整异常如下,完整源码在github上:https://github.com/abhibeckert/Dux/blob/master/Dux/DuxTextView.m
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to convert bytes in string 0x10004d430 to _NSCStringEncoding'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff84afb286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff88991d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff84afb0ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff84afb044 +[NSException raise:format:] + 116
4 Foundation 0x00007fff835bfae4 copyFromStringToStorage + 262
5 Foundation 0x00007fff835bf979 -[NSBigMutableString replaceCharactersInRange:withString:] + 1000
6 Foundation 0x00007fff835bc3f7 -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 375
7 AppKit 0x00007fff86149e14 -[NSConcreteTextStorage replaceCharactersInRange:withString:] + 81
8 Dux 0x0000000100002f9a -[MyTextDocument loadTextContentIntoStorage] + 1338
9 Dux 0x00000001000022a0 -[MyTextDocument windowControllerDidLoadNib:] + 640
10 AppKit 0x00007fff860f1328 -[NSWindowController _windowDidLoad] + 667
11 AppKit 0x00007fff860e89a3 -[NSWindowController window] + 109
12 AppKit 0x00007fff8615d761 -[NSDocument windowForSheet] + 86
13 AppKit 0x00007fff860e82c4 -[NSDocument _shouldShowAutosaveButtonForWindow:] + 50
14 AppKit 0x00007fff860e7fbb -[NSWindowController setDocument:] + 237
15 AppKit 0x00007fff8629c9b6 -[NSDocument makeWindowControllers] + 139
16 AppKit 0x00007fff8615d555 -[NSDocument(NSPersistentUISupport) restoreDocumentWindowWithIdentifier:state:completionHandler:] + 90
17 AppKit 0x00007fff8615d4aa -[NSDocumentControllerPersistentRestoration loadedDocument:forAutoID:] + 179
18 AppKit 0x00007fff8615cfbe __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_8 + 187
19 AppKit 0x00007fff86148e14 __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_5 + 163
20 AppKit 0x00007fff86148d5f __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_4 + 697
21 AppKit 0x00007fff86148aa1 -[NSDocumentController _openDocumentWithContentsOfURL:usingProcedure:] + 530
22 AppKit 0x00007fff8614868d __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_3 + 242
23 libdispatch.dylib 0x00007fff8bbbe8ba _dispatch_call_block_and_release + 18
24 libdispatch.dylib 0x00007fff8bbc072a _dispatch_main_queue_callback_4CF + 308
25 CoreFoundation 0x00007fff84a904dc __CFRunLoopRun + 1724
26 CoreFoundation 0x00007fff84a8fae6 CFRunLoopRunSpecific + 230
27 HIToolbox 0x00007fff8852f3d3 RunCurrentEventLoopInMode + 277
28 HIToolbox 0x00007fff8853663d ReceiveNextEventCommon + 355
29 HIToolbox 0x00007fff885364ca BlockUntilNextEventMatchingListInMode + 62
30 AppKit 0x00007fff85ef23f1 _DPSNextEvent + 659
31 AppKit 0x00007fff85ef1cf5 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
32 AppKit 0x00007fff85eee62d -[NSApplication run] + 470
33 AppKit 0x00007fff8616d80c NSApplicationMain + 867
34 Dux 0x0000000100001e32 main + 34
35 Dux 0x0000000100001e04 start + 52
36 ??? 0x0000000000000003 0x0 + 3
)
【问题讨论】:
-
您可能有内存损坏。您是否尝试过运行 XCode 中可用的内存一致性工具? (构建和分析、僵尸等)
-
是的,我已经尝试构建和分析,并且启用了僵尸。我也在使用 ARC。
-
我去拿了你的项目,因为它很吸引人。我启用了涂鸦保护和所有这些,仍然没有任何问题。分析器显示了许多有效的问题,但修复它们并不会影响此问题。
-
我能想到的唯一想法是,您的应用程序在正确加载文档时会进入某种“状态”,这会影响它在下次运行时的启动(但仅在调试器中的调试中?)。如果它崩溃,它不会执行加载行之后的任何代码。我尝试通过在每次运行的连续行上杀死它来查看它是否是函数中的一行导致它,但如果你这样做它仍然有效。导致问题的原因是应用程序中的其他地方。我希望这会有所帮助!
-
令人着迷!根据App Sandbox Design Guide,您应该能够通过从控制台运行
sandboxd来询问您的应用程序在沙盒模式下运行时的情况。当您这样做时,您是否看到报告的任何违规行为?
标签: objective-c macos cocoa nstextstorage