gpt4 book ai didi

iphone - CFAttributedStringSetAttribute 和 NSNumber 中的 EXC_BAD_ACCESS?

转载 作者:搜寻专家 更新时间:2023-10-30 20:01:53 25 4
gpt4 key购买 nike

我正在处理的 Objective-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

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

谢谢,理查德

最佳答案

您的崩溃看起来像是某处过度释放的结果。很难找出问题所在,尤其是当您将 core foundation 与 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)

关于iphone - CFAttributedStringSetAttribute 和 NSNumber 中的 EXC_BAD_ACCESS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2985380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com