1

これは非常に奇妙に聞こえますが、このスタックトレースが示唆しているようです。

何か案は?

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ツールを使用してメモリリークを見つける方法を実際に学ぶ時が来たと思います。

4

0 に答える 0