这听起来非常非常奇怪,但似乎是这个堆栈跟踪所暗示的。
有任何想法吗?
Thread: Unknown Name (Crashed)
0 0 libsystem_kernel.dylib 0x38a79350 __pthread_kill + 8
1 1 libsystem_c.dylib 0x34002973 abort + 94
2 2 libsystem_c.dylib 0x33fccf19 <redacted>
3 3 libsystem_c.dylib 0x33fcd1dd <redacted> + 28
4 4 libsystem_c.dylib 0x33f9977b <redacted> + 214
5 5 libsystem_c.dylib 0x33f98773 <redacted> + 1198
6 6 libsystem_c.dylib 0x33f98293 malloc_zone_malloc + 70
7 7 CoreFoundation 0x36a32a10 <redacted> + 252
8 8 CoreFoundation 0x36a33e4c <redacted> + 92
9 9 CoreFoundation 0x3695b777 CFDictionarySetValue + 166
10 10 CoreFoundation 0x36a27467 <redacted> + 102
11 11 CoreFoundation 0x369a2347 CFCalendarCreateWithIdentifier + 534
12 12 CoreFoundation 0x36a06f8b <redacted> + 106
13 13 CoreFoundation 0x369ab021 _CFLogvEx + 188
14 14 Foundation 0x347a5d8f NSLogv + 86
15 15 Foundation 0x347a5d2f NSLog + 26
16 16 TypOHD 0x000efe5f -[TypoViewControllerBase markIt:] (TypoViewControllerBase.m:325)
这就是奇怪的原因:方法中的所有 NSLog 调用都使用静态 NSString 常量。这是方法:
- (IBAction) markIt:(id)sender
{
NSLog(@"Marking it");
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSRange visible = [self visibleRangeOfTextView:self.source];
NSRange last = NSMakeRange(visible.location, 0);
// int cursorPos = self.textView.selectedRange.location;
NSString *text = self.source.text;
while (true) {
NSRange error = [controller rangeOfMisspelledWordInString:text onlyInRange:visible startingAt:last.location + last.length];
last = error;
if (error.location == NSNotFound || error.length == 0) {
break;
}
[arr addObject:[NSValue valueWithRange:error]];
}
NSLog(@"Spellchecked");
UIGraphicsBeginImageContextWithOptions(self.overlay.frame.size, NO, 0);
[[UIColor redColor] setStroke];
UITextPosition *current = self.source.beginningOfDocument;
int curTextPos = 0;
CGFloat dash[] = {2.0f, 2.0f} ;
int sourceLen = text.length;
// Make the text ranges and mark them
for (NSValue *val in arr) {
NSRange range = [val rangeValue];
if (range.location + range.length >= sourceLen) {
break;
}
int toMove = range.location - curTextPos;
UITextPosition *start = [self.source positionFromPosition:current offset:toMove];
current = start;
curTextPos = range.location;
UITextPosition *end = [self.source positionFromPosition:start offset:range.length];
UITextRange *textRange = [self.source textRangeFromPosition:start toPosition:end];
CGRect rectInTextView = [self.source firstRectForRange:textRange];
CGRect toDraw = rectInTextView;
toDraw.origin.y -= self.source.contentOffset.y;
UIBezierPath* aPath = [UIBezierPath bezierPath];
[aPath setLineDash:dash count:2 phase:0];
CGPoint lineStart = toDraw.origin;
lineStart.y += toDraw.size.height;
CGPoint lineEnd = lineStart;
lineEnd.x += toDraw.size.width;
[aPath moveToPoint:lineStart];
[aPath addLineToPoint:lineEnd];
[aPath stroke];
};
self.overlay.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"Done");
}
编辑:
一些在日志中的挖掘表明这是一种内存不足的情况,导致后来的怪异。但我怀疑内存泄漏并不是这种方法特别引起的。据我所知,它正确地释放了它的上下文,对吧?
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGBitmapContextInfoCreate: unable to allocate 8552448 bytes for bitmap data
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSaveGState: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineWidth: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineJoin: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineCap: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetMiterLimit: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetFlatness: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextSetLineDash: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextAddPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextDrawPath: invalid context 0x0
Oct 2 20:23:55 Admins-MacBook-Pro.local SkrivestotteHD[35780] <Error>: CGContextRestoreGState: invalid context 0x0
2
在 malloc_error_break 中设置断点告诉我内存不足的情况是由这一行引起的:
CGRect rectInTextView = [self.source firstRectForRange:textRange];
或者更确切地说,特别是 firstRectForRange 方法。
但是:注释掉整个 markIt 方法只会将内存不足的情况移到其他地方。我想是时候真正学习如何使用 XCode 工具来查找内存泄漏了。