質問

シンプルなテキスト エディター (Lion の新しいドキュメント ベースのアプリ テンプレートを使用し、カスタマイズはほとんどありません) 用の NSDocument サブクラスがありますが、ファイル コンテンツをテキスト ストレージにロードする際に奇妙なバグが発生しています。

私のコードは次のとおりです。

- (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 に変換できません」。

しかし、それはすべての場合にのみ発生します 2番 アプリの起動 (3 回目の起動ではクラッシュせず、 意思 前回開こうとしてクラッシュしたドキュメントを自動的に再度開きます)。また、Xcode からアプリを実行する場合にのみ発生します。リリース ビルドが起動時にクラッシュしたことはありません。

自動保存システムのエンコードの問題かもしれないと思いましたが、そのコードをコメントアウトして @"hello world" 文字列をテキスト ビューにロードするだけでもクラッシュします (上記のコードに示すように)。同様に、コメントアウトされた NSLog() 何も変なものは表示されません。テキスト ストレージは有効 (xib ファイルからロード)、テキスト ストレージの長さは 0、textContent は開かれているファイルの内容です。

- - 編集 - -

この問題が何らかの形で関連していることがわかりました。 com.apple.security.app-sandbox 権利。権利/サンドボックスが 有効, 、次に私のアプリ ではない クラッシュ。資格またはアプリサンドボックス機能のいずれかが 無効, 、その後、以前に開いたドキュメントを復元しようとすると、2回目の起動ごとにアプリがクラッシュします。

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
)
役に立ちましたか?

解決 3

これはOSX 10.7(Lion)のバグのようです。コードに変更を加えることなく、Xcode4.4.1に準拠したOSX 10.8(Mountain Lion)でクラッシュが発生しなくなりました。

他のヒント

正直に言うと、私はこの質問にかなり長い間困惑していました。ようやく再現品を見つけた気がします。

を通して見ると GNUstep この動作をエミュレートするコード, 次のソースコードを見つけました。

if (enc == NSASCIIStringEncoding
    && isByteEncoding(internalEncoding))
    {
      unsigned  i;

      if (bytes > self->_count)
    {
      bytes = self->_count;
    }
      for (i = 0; i < bytes; i++)
    {
      unsigned char c = self->_contents.c[i];

      if (c > 127)
        {
          [NSException raise: NSCharacterConversionException
              format: @"unable to convert to encoding"];
        }
      buffer[i] = c;
    }
      buffer[bytes] = '\0';
      if (bytes < self->_count)
    {
      return NO;
    }
      return YES;
    }
...

明らかに、このコードはエラー メッセージと 1 対 1 に対応していません。 しかし、 エラーの再現からこれに注意してください:

reason: 'Unable to convert bytes in string 0x10004d430 to_NSCStringEncoding'

良い、 C String が何であるかを誰もが知っています, 間違ったエンコード方法 (たとえば、ASCII が期待される場所で Wide を使用したり、その逆) を使用すると、まさにこの問題が発生することになります。したがって:この問題を解決するために、アプリケーションで ASCII および Wide データ型を変更してみましたか?

この動作が非常に一般的であるため、これはアプリケーション サンドボックスによって処理されたケースである可能性が高くなります。ただし、これに関する文書はまだ見つかりません。これは、によって明示的に指摘されるべきです sandboxd, 、質問の正誤表で挙げたように。

10.9.2でも同じ例外が発生しました。 ジェネラコディセタグプレ

私はNSTextStorageapplicationWillFinishLaunchingを使用していました。私はそれをapplicationDidFinishLaunchingに移し、正常に機能しています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top