2011-12-06 5 views
20

간단한 텍스트 편집기 (사용자 정의가 거의없는 Lion의 새 문서 기반 앱 템플리트 사용)에 대한 NSDocument 하위 클래스가 있으며 파일 내용을 텍스트 저장소로로드하는 이상한 버그가 발생합니다. NSTextStorage에서 혼란스러운 인코딩 예외가 발생했습니다.

  • 이 응용 프로그램을 종료 문서를 열
    • 이 (디버그 빌드로) 엑스 코드에서 응용 프로그램을 실행

      : 나는 경우

      - (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에서 앱을 다시 실행하십시오.

    -replaceCharactersInRange:withString:에서 "문자열 0x10004d430의 바이트를 _NSCStringEncoding으로 변환 할 수 없습니다"라는 오류가 발생합니다.

    는하지만 그것은 단지 응용 프로그램의 모든 발사에서 발생 (세 번째 발사가 중단되지 않으며, 그것은 는 자동으로 이전의 시간을 열려고 추락 문서를 다시 열 것). 또한 Xcode에서 앱을 실행할 때만 발생합니다. 릴리스 빌드는 실행시 충돌하지 않습니다.

    자동 저장 시스템에서 인코딩 문제 일 수 있다고 생각했지만 코드를 주석 처리하고 텍스트 뷰에 @ "hello world"문자열을로드 할 때 충돌이 발생합니다 (위의 코드 참조) . 비슷하게, 주석 처리 된 NSLog()은 이상한 것을 보여주지 않습니다. 텍스트 저장소는 유효하며 (xib 파일에서로드 됨) 텍스트 저장 길이는 0이고 textContent는 열려있는 파일의 내용입니다.

    --- 편집 ---

    나는이 문제는 com.apple.security.app-sandbox 자격에 어떤 식 으로든 관련이있다 배웠다. 인 타이틀먼트/샌드 박스가 인 경우 인 경우 내 이 아닙니다. 자격 또는 app-sandbox 기능 중 하나가 이 비활성화 된 경우 인 경우 이전에 연 문서를 복원하려고 시도 할 때마다 내 앱이 충돌합니다.

    xcode에서 빌드/실행을 할 때 충돌이 발생했는데, 이는 샌드 박스가 비활성화 된 유일한 빌드 구성 이었기 때문입니다. ---/EDIT

    ---

    사람이 어떤 아이디어가 있습니까? 전체 예외는 다음과 전체 소스 코드는 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 
    ) 
    
  • +0

    메모리가 손상되었을 수 있습니다. XCode에서 사용할 수있는 메모리 일관성 도구를 사용해 보셨습니까? (Build and Analyze, Zombies, and ilk) – voidref

    +0

    예, 빌드 및 분석을 시도했으며 좀비가 활성화되었습니다. ARC도 사용하고 있습니다. –

    +0

    매혹적이어서 프로젝트에 참여했습니다. 나는 낙서 가드와 그 모든 것들을 가능하게했는데, 여전히 틀린 것을 찾지 못했습니다. 분석기에 표시되는 여러 가지 정당한 우려 사항이 있지만 문제를 수정해도이 문제에 영향을 미치지 않습니다. – voidref

    답변

    0

    이것은 OS X 10.7 (Lion)의 버그로 보입니다. 내 코드가 전혀 변경되지 않아 Xcode 4.4.1을 준수하는 OS X 10.8 (Mountain Lion)에서 충돌이 더 이상 발생하지 않습니다.

    +0

    OS X 10.9.1에서 Xcode 5.0.2의 프로젝트 중 하나에서이 문제가 발생합니다. 그게 당신을 위해 다시 등장한 건지 궁금 해서요? –

    +0

    @CharlesA. 그것은 나를 위해 재현되지 않았지만 어쩌면 같은 증상을 유발할 수있는 다른 방법이있을 수 있습니다. –

    5

    는,이 질문은 아주 잠시 나를 난처한 상황에 빠진. 나는 마침내 책략을 찾았다 고 생각한다.이 코드는 일대일 당신의 오류 메시지가 아닌, 분명히

    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; 
        } 
    ... 
    

    다음 GNUstepcode that emulates this behavior 통해 찾고

    , 나는 다음과 같은 소스 코드를 발견했다. 그러나, 내가 당신의 오류 생식에서이주의 :

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

    음, we all know what a C String is, 당신은 (예를 들어 잘못된 인코딩 방법을 사용하는 경우, 와이드 ASCII가 예상되는 경우 또는 그 반대의 경우도 마찬가지 임), 당신은이 문제로 끝날 것입니다. 따라서 :이 문제를 해결하기 위해 응용 프로그램에서 다양한 ASCII 및 Wide 데이터 형식을 시도해 보셨습니까?

    이 문제는 일반적으로 발생하기 때문에 응용 프로그램 샌드 박스에서 처리되는 사례 일 가능성이 큽니다. 그러나 아직이 효과에 대한 문서를 찾을 수 없습니다. 이 질문은 정오표에서 제기 한 바와 같이 명시 적으로 sandboxd에 표시된 것으로해야합니다.

    +1

    글쎄, Abhi는이 문제로 이끄는 지 아직 알지 못했지만 현상금이 곧 만료 될 예정이며, 당신의 대답은 잘 생각되어지고 적어도 도움이된다. 그래서 나는 현상금을 당신에게 줄 것이다. –

    +0

    도움을 주셔서 감사합니다. 여분의 시간이있을 때 살펴 보겠습니다. 끔찍한 낙점은 아니지만, 파일에서 문자열을 읽을 때뿐만 아니라 필드에 하드 코딩 된 @ "hello world"문자열 상수를 필드에 삽입 할 때도 충돌이 발생하기 때문에.이것을 디버깅 할 다음 생각은 예외가 문자열 필드에 대해 이야기하는 것이 아니라 이미 텍스트 필드에있는 문자열 객체이며 새로운 값으로 대체된다는 것입니다. 코코아의 자동 저장 기능이 이상한 일을하고있을 수도 있습니다. 나는 실험 할 시간이 없었다. –

    +0

    나는 그것이 무엇인지 밝혀내는 것에 흥미가있을 것이다. 나는 그것을 허용 할 수있는 한 문서화 할 수있는 범위까지 되돌려 보았지만 Xcode에 던져 넣고 무슨 일이 일어나는 지 알 수있었습니다. 간단한 문자 인코딩 문제라고 생각하지만 _where_와 _why_는 여기서 가장 중요한 부분입니다. – MrGomez

    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 
    ) 
    

    나는 applicationWillFinishLaunchingNSTextStorage를 사용했다. 나는 이것을 applicationDidFinishLaunching으로 옮겼으며 그 잘 작동한다.

    관련 문제