objective-c – drawRect中的崩溃 – 是什么原因?

前端之家收集整理的这篇文章主要介绍了objective-c – drawRect中的崩溃 – 是什么原因?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的一些用户正在崩溃.

据我所知,这与某些子类的NSTextView的-drawRect:方法连接起来,但是我看不到可能会导致什么,压力测试未能摆脱错误.

drawRect代码

  1. - (NSRange)visibleRangeOfTextView:(NSRect) rect {
  2. NSLayoutManager *layoutManager = [self
  3. layoutManager];
  4. NSTextContainer *textContainer = [self
  5. textContainer];
  6. NSRange glyphRange,characterRange;
  7. // first transform to text container coordinates
  8. NSPoint containerOrigin = [self textContainerOrigin];
  9. rect.origin.x -= containerOrigin.x;
  10. rect.origin.y -= containerOrigin.y;
  11.  
  12. // next,compute glyph range
  13. glyphRange = [layoutManager glyphRangeForBoundingRect:rect inTextContainer:textContainer];
  14.  
  15. // finally,compute character range
  16. characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
  17. return characterRange;
  18. }
  19.  
  20. - (NSRect)rectForCharacterRange:(NSRange)charRange
  21. {
  22. NSRect rect = [self
  23. firstRectForCharacterRange:charRange];
  24. rect.origin = [[self window]
  25. convertScreenToBase:rect.origin];
  26. rect = [self convertRect:rect fromView:nil];
  27. if (!rect.size.width) rect.size.width = 6.0;
  28. return rect;
  29. }
  30.  
  31. - (void)drawRect:(NSRect)dirtyRect
  32. {
  33. [super drawRect:dirtyRect];
  34. NSLog(@"Marking it");
  35. NSMutableArray *arr = [[NSMutableArray alloc] init];
  36. NSRange visible = [self visibleRangeOfTextView:dirtyRect];
  37. NSRange last = NSMakeRange(visible.location,0); while (true) {
  38. NSRange error = [appController rangeOfMisspelledWordInString:self.string onlyInRange:visible startingAt:last.location + last.length];
  39. last = error;
  40. if (error.location == NSNotFound) {
  41. break;
  42. }
  43. [arr addObject:[NSValue valueWithRange:error]];
  44. }
  45.  
  46. NSLog(@"Spellchecked");
  47.  
  48. [[NSColor redColor] setStroke];
  49. CGFloat dash[] = {2.0f,2.0f} ;
  50. // Make the text ranges and mark them
  51. for (NSValue *val in arr) {
  52. NSRange range = [val rangeValue];
  53. NSRect rectInTextView = [self rectForCharacterRange:range];
  54. NSRect toDraw = rectInTextView;
  55. NSBezierPath* aPath = [NSBezierPath bezierPath];
  56. [aPath setLineDash:dash count:2 phase:0];
  57.  
  58. NSPoint lineStart = toDraw.origin;
  59. lineStart.y += toDraw.size.height;
  60. NSPoint lineEnd = lineStart;
  61. lineEnd.x += toDraw.size.width;
  62.  
  63. [aPath moveToPoint:lineStart];
  64. [aPath lineToPoint:lineEnd];
  65. [aPath stroke];
  66. };
  67. NSLog(@"Done");
  68. }

堆栈跟踪:

  1. Crashed Thread: 0 Dispatch queue: com.apple.main-thread
  2.  
  3. Exception Type: EXC_CRASH (SIGABRT)
  4. Exception Codes: 0x0000000000000000,0x0000000000000000
  5.  
  6. Application Specific Information:
  7. objc[5751]: garbage collection is ON
  8. *** Terminating app due to uncaught exception 'NSRangeException',reason: '*** -[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:]: Range or index out of bounds'
  9. *** First throw call stack:
  10. (
  11. 0 CoreFoundation 0x00007fff91387fc6 __exceptionPreprocess + 198
  12. 1 libobjc.A.dylib 0x00007fff8d4d7d5e objc_exception_throw + 43
  13. 2 CoreFoundation 0x00007fff91387dfa +[NSException raise:format:arguments:] + 106
  14. 3 CoreFoundation 0x00007fff91387d84 +[NSException raise:format:] + 116
  15. 4 AppKit 0x00000001005d842c -[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:] + 131
  16. 5 AppKit 0x00000001006288ec -[NSLayoutManager(NSPrivate) _drawBackgroundForGlyphRange:atPoint:parameters:] + 910
  17. 6 AppKit 0x00000001006277a2 -[NSTextView drawRect:] + 1913
  18. 7 Skrivest√∏tte 0x000000010000b56c Skrivest√∏tte + 46444
  19. 8 AppKit 0x0000000100626e66 -[NSTextView _drawRect:clip:] + 2545
  20. 9 AppKit 0x00000001004a985d -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 3020
  21. 10 AppKit 0x00000001004aa34e -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 5821
  22. 11 AppKit 0x00000001004aa34e -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 5821
  23. 12 AppKit 0x00000001004a39af -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 4755
  24. 13 AppKit 0x000000010049c395 -[NSView displayIfNeeded] + 1528
  25. 14 AppKit 0x00000001004a1592 -[NSClipView _immediateScrollToPoint:] + 6533
  26. 15 AppKit 0x000000010049fb75 -[NSClipView scrollToPoint:] + 239
  27. 16 AppKit 0x000000010058f637 -[NSScrollView scrollClipView:toPoint:] + 266
  28. 17 AppKit 0x000000010058f3da -[NSClipView _scrollTo:animateScroll:flashScrollerKnobs:] + 1497
  29. 18 AppKit 0x00000001005923b7 -[NSClipView _scrollTo:animate:] + 27
  30. 19 AppKit 0x0000000100bcd5a2 __-[NSScrollView _snapRubberBand]_block_invoke_2 + 1536
  31. 20 AppKit 0x0000000100b3fc4e ____NSPeriodicInvokerScheduled_block_invoke_2 + 53
  32. 21 libdispatch.dylib 0x00007fff907b98ba _dispatch_call_block_and_release + 18
  33. 22 libdispatch.dylib 0x00007fff907bbc07 _dispatch_after_timer_callback + 16
  34. 23 libdispatch.dylib 0x00007fff907be2b6 _dispatch_source_invoke + 635
  35. 24 libdispatch.dylib 0x00007fff907baf77 _dispatch_queue_invoke + 71
  36. 25 libdispatch.dylib 0x00007fff907bb6f7 _dispatch_main_queue_callback_4CF + 257
  37. 26 CoreFoundation 0x00007fff9131d06c __CFRunLoopRun + 1724
  38. 27 CoreFoundation 0x00007fff9131c676 CFRunLoopRunSpecific + 230
  39. 28 HIToolBox 0x00007fff93ab831f RunCurrentEventLoopInMode + 277
  40. 29 HIToolBox 0x00007fff93abf5c9 ReceiveNextEventCommon + 355
  41. 30 HIToolBox 0x00007fff93abf456 BlockUntilNextEventMatchingListInMode + 62
  42. 31 AppKit 0x000000010045ff5d _DPSNextEvent + 659
  43. 32 AppKit 0x000000010045f861 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
  44. 33 AppKit 0x000000010045c19d -[NSApplication run] + 470
  45. 34 AppKit 0x00000001006dab88 NSApplicationMain + 867
  46. 35 Skrivest√∏tte 0x0000000100001020 Skrivest√∏tte + 4128
  47. 36 ??? 0x0000000000000002 0x0 + 2
  48. )

解决方法

*由于未被捕获的异常’NSRangeException’终止应用程序,原因:’*

[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:]: Range or index out of bounds’

这显然是一个NSRange异常,正如O’y所说

猜你在找的C&C++相关文章