3

我正在开发的目标 c 应用程序中出现令人愤怒的 EXC_BAD_ACCESS 错误。您能提供的任何帮助将不胜感激。我已经尝试了针对此错误的正常调试方法(打开 NSZombieEnabled,检查保留/释放/自动释放以确保我没有尝试访问已释放的对象等),但它似乎没有帮助。

基本上,错误总是发生在这个函数中:

void op_TJ(CGPDFScannerRef scanner, void *info)
{
    PDFPage *self = info;
    CGPDFArrayRef array;

    NSMutableString *tempString = [NSMutableString stringWithCapacity:1];
    NSMutableArray *kernArray = [[NSMutableArray alloc] initWithCapacity:1];

    if(!CGPDFScannerPopArray(scanner, &array)) {
        [kernArray release];
        return;
    }

    for(size_t n = 0; n < CGPDFArrayGetCount(array); n += 2)
    {
        if(n >= CGPDFArrayGetCount(array))
            continue;

        CGPDFStringRef pdfString;

        // if we get a PDF string
        if (CGPDFArrayGetString(array, n, &pdfString))
        {
            //get the actual string
            const unsigned char *charstring = CGPDFStringGetBytePtr(pdfString);

            //add this string to our temp string
            [tempString appendString:[NSString stringWithCString:(const    char*)charstring encoding:[self pageEncoding]]];
            //NSLog(@"string: %@", tempString);

            //get the space after this string
            CGPDFReal r = 0;
            if (n+1 < CGPDFArrayGetCount(array)) {
                CGPDFArrayGetNumber(array, n+1, &r);

                // multiply by the font size
                CGFloat k = r;
                k = -k/1000 * self.tmatrix.a * self.fontSize;


                CGFloat kKern = self.kern * self.tmatrix.a;
                k = k + kKern;

                // add the location and kern to the array
                NSNumber *tempKern = [NSNumber numberWithFloat:k];
                NSLog(@"tempKern address: %p", tempKern);
                [kernArray addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:[tempString length] - 1], tempKern, nil]];

            }
        }
    }

    // create an attribute string
    CFMutableAttributedStringRef attString =    CFAttributedStringCreateMutable(kCFAllocatorDefault, 10);

    CFAttributedStringReplaceString(attString, CFRangeMake(0, 0), (CFStringRef)tempString);

    //apply overall kerning
    NSNumber *tkern = [NSNumber numberWithFloat:self.kern * self.tmatrix.a * self.fontSize];
    CFAttributedStringSetAttribute(attString, CFRangeMake(0, CFAttributedStringGetLength(attString)), kCTKernAttributeName, (CFNumberRef)tkern);

    //apply individual kern attributes
    for (NSArray *kernLoc in kernArray) {
        NSLog(@"kern location: %i, %i", [[kernLoc objectAtIndex:0] intValue],[[kernLoc objectAtIndex:1] floatValue]);
        CFAttributedStringSetAttribute(attString, CFRangeMake([[kernLoc objectAtIndex:0] intValue], 1), kCTKernAttributeName, (CFNumberRef)[kernLoc objectAtIndex:1]);
    }

    CFAttributedStringReplaceAttributedString([self cfAttString], CFRangeMake(CFAttributedStringGetLength([self cfAttString]), 0), attString);

    //release
    CFRelease(attString);
    [kernArray release];


}

程序总是因为线路崩溃

CFAttributedStringSetAttribute(attString, CFRangeMake([[kernLoc objectAtIndex:0] intValue], 1), kCTKernAttributeName, (CFNumberRef)[kernLoc objectAtIndex:1])

它似乎取决于几件事:

  1. 如果 [kernLoc objectAtIndex:1] 指的是 [NSNumber numberWithFloat:k],其中 k = 0(换句话说,如果 k = 0 高于我填充 kernArray 的位置),则程序几乎立即崩溃

  2. 如果我注释掉 k = k + kKern 行,程序崩溃需要更长的时间,但最终会崩溃(为什么崩溃取决于这个值?)

  3. 如果我将 CFRangeMake 的长度从 1 更改为 0,程序崩溃需要更长的时间,但最终还是会崩溃。(我不认为我试图超出attString的范围,但我错过了什么吗?)

当它崩溃时,我得到类似于:

#0  0x942c7ed7 in objc_msgSend ()
#1  0x00000013 in ?? ()
#2  0x0285b827 in CFAttributedStringSetAttribute ()
#3  0x0000568f in op_TJ (scanner=0x472a590, info=0x4a32320) at /Users/Richard/Desktop/AppTest/PDFHighlight 2/PDFScannerOperators.m:251

有任何想法吗?似乎在某个地方我正在覆盖内存或尝试访问已更改的内存,但我不知道。如果我可以提供更多信息,请告诉我。

谢谢,理查德

4

2 回答 2

4

您的崩溃看起来像是某处过度释放的结果。很难找出问题可能是什么,尤其是当您将核心基础与 Cocoa 混合时。核心基础的内存管理规则是不同的。

我想我会从 CFAttributedStringSetAttribute 中提取所有对数据的引用,这样我就可以 NSLog 它们或使用调试器检查它们,如下所示:

NSNumber* rangeStart = [kernLoc objectAtIndex:0];      // Debug: make sure it is a number
NSNumber attrValue = [kernLoc objectAtIndex:1];        // Debug: make sure it is a number
CFRange range = CFRangeMake([rangeStart intValue], 1); // Debug: make sure it is a valid range for the string
CFAttributedStringSetAttribute(attString, range, kCTKernAttributeName, (CFNumberRef)attrValue)
于 2010-06-07T09:17:59.877 回答
2

跟进 - 似乎 CFAttributedStringSetAttributes (复数)是要走的路。我仍然需要在我的程序中实现它,但是在一个小测试中,这个函数允许我覆盖以前定义的值而不会导致 EXC_BAD_ACCESS 崩溃。如果这被记录在某处会很好。希望这可以帮助其他人。谢谢!

于 2010-06-08T08:06:39.110 回答