title: Confusing encoding exception in NSTextStorage
body: <p>I have an NSDocument subclass for a simple text editor (using Lion's new document based app template, with few customisations), and I'm encountering a strange bug loading the file content into the text storage.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>The bug happens when I:</p>
  
  <ul>
  <li>run the app in Xcode (as a debug build)</li>
  <li>open any document</li>
  <li>quit the app (without closing the document)</li>
  <li>run the app again from Xcode</li>
  </ul>
  
  <p>It crashes in <code>-replaceCharactersInRange:withString:</code> with "Unable to convert bytes in string 0x10004d430 to _NSCStringEncoding".</p>
  
  <p>But it only happens on every <em>second</em> launch of the app (a third launch will not crash, and it <em>will</em> automatically re-open the document it crashed trying to open the previous time). It also only happens when I run the app from Xcode. Release builds have never crashed on launch.</p>
  
  <p>I thought it might be an encoding issue with the auto-save system, but it even crashes when I comment out that code and just load @"hello world" string into the text view (as shown in the above code). Similarly, the commented out <code>NSLog()</code> doesn't show anything weird. The text storage is valid (loaded from the xib file), the text storage length is 0, and the textContent is the contents of the file being opened.</p>
  
  <p><strong><em>--- EDIT ---</em></strong></p>
  
  <p>I've learned this problem is related in some way to the <code>com.apple.security.app-sandbox</code> entitlement. If entitlements / sandbox are <strong>enabled</strong>, then my app <strong>does not</strong> crash. If either entitlements or the app-sandbox feature are <strong>disabled</strong>, then my app crashes on every second launch trying to restore previously opened documents.</p>
  
  <p>I had only noticed it crashing when doing a build/run from inside xcode, because that was my only build configuration with the sandbox disabled.</p>
  
  <p><strong><em>--- /EDIT ---</em></strong></p>
  
  <p>Does anyone have any ideas? The full exception follows, and the full source code is on github: <a href="https://github.com/abhibeckert/Dux/blob/master/Dux/DuxTextView.m" rel="noreferrer">https://github.com/abhibeckert/Dux/blob/master/Dux/DuxTextView.m</a></p>
  
  <pre><code>*** 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
  )
  </code></pre>