20

単純なテキストエディター用の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に変換できません」でクラッシュします。

ただし、これはアプリの2回目の起動ごとにのみ発生します(3回目の起動はクラッシュせず、前回開いたときにクラッシュしたドキュメントを自動的に再度開きます)。また、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
)
4

3 に答える 3

5

私は認めます、この質問はかなり長い間私を困惑させました。でも、ようやく再現を見つけたと思います。

この振る舞いをエミュレートする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文字列が何であるかを知っています。間違ったエンコード方法を使用すると(たとえば、ASCIIが期待される場合はWide、またはその逆)、まさにこの問題が発生します。したがって、この問題を修正するために、アプリケーションでさまざまなASCIIおよびワイドデータ型を試しましたか?

この動作は非常に一般的であるため、アプリケーションサンドボックスによって処理されたケースである可能性が非常に高くなります。ただし、この効果に関するドキュメントはまだ見つかりません。sandboxdこれは、正誤表で提起したように、によって明示的に示されているはずです。

于 2012-04-01T22:03:33.240 に答える
1

10.9.2でも同じ例外が発生しました。

 Unable to convert bytes in string 0x60800026cd40 to _NSCStringEncoding
 (
    0   CoreFoundation                      0x00007fff8dec825c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff81de0e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8dec810c +[NSException raise:format:] + 204
    3   Foundation                          0x00007fff8d94dd2b copyFromStringToStorage + 223
    4   Foundation                          0x00007fff8d94db9e -[NSBigMutableString replaceCharactersInRange:withString:] + 1081
    5   Foundation                          0x00007fff8d94d738 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 296
    6   AppKit                              0x00007fff88d7889c -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 77
    9   myApp                            0x00000001035d3fff myApp + 12287
    10  CoreFoundation                      0x00007fff8de96e0c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    11  CoreFoundation                      0x00007fff8dd8a82d _CFXNotificationPost + 2893
    12  Foundation                          0x00007fff8d8f8e4a -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
    13  AppKit                              0x00007fff88b99cde -[NSApplication finishLaunching] + 440
    14  AppKit                              0x00007fff88b997f3 -[NSApplication run] + 128
    15  AppKit                              0x00007fff88b84783 NSApplicationMain + 940
    16  libdyld.dylib                       0x00007fff81aa65fd start + 1
    17  ???                                 0x0000000000000001 0x0 + 1
)

で使用NSTextStorageしていましたapplicationWillFinishLaunching。私はそれをに移動し、applicationDidFinishLaunching正常に動作しています。

于 2015-05-27T10:00:32.770 に答える
0

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

于 2012-08-16T19:05:49.127 に答える