- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在处理的 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])
这似乎取决于一些事情:
如果 [kernLoc objectAtIndex:1] 引用了一个 [NSNumber numberWithFloat:k],其中 k = 0(换句话说,如果 k = 0 高于我填充 kernArray 的位置),那么程序几乎立即崩溃
如果我注释掉 k = k + kKern 行,程序崩溃需要更长的时间,但最终会崩溃(为什么崩溃取决于这个值?)
如果我将 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/
我正在开发一些代码,其中我使用扫描仪从字符串中获取 NSNumbers,例如 x 和 y。 现在我想从 x 和 y 计算一些简单的东西,比如 z = 10.0/(x + y/60.0)/60.0)。我
第一次发帖。 NSNumber 的 floatValue 方法有问题——不知何故,它返回一个不精确的数字。问题在于:我将一堆 NSNumbers 存储在一个数组中,如下所示: NSArray *a =
我在尝试使用从基类继承的泛型函数的参数时遇到标题错误 基类: BaseBottomBar: UIView { ... func formatDetailText(value: T...
我有下面这行代码 NSNumber *myValue = loadTempValue*0.420; 我试图将 *myValue 的值设置为 loadTempValue*0.420 的值, 但是,我得到
我在将 NSNumber 转换为字符串以及将字符串转换为 NSNumber 时遇到问题。 这是一个示例问题: NSString *stringValue = @"9.2"; NSNumberForma
有一个坐标对象,具有三个变量纬度(NSNumber),经度(NSNumber)和时间(NSDate),为了在我的模拟器上检查程序,我给出了以下代码 [coordinate setLatitude:[N
简单方法:我有一个 SearchView,用户可以在其中选择一些 CoreDate,然后他可以搜索 Name、City( >NSString) 和Clientnumber (NSNumber) NSP
我转换这个 Objective-C 函数: - (NSArray *)grt_map:(id (^)(id))block { NSMutableArray *newArray = [NSMut
示例:我有一个 NSInteger,我将其包装到一个 NSNumber 对象中。现在我想要一个具有该 NSInteger 值的 NSDecimal。 那么我可以问: NSDecimal myDecim
我有这个自定义类: @interface MyModel : NSObject @property (nonatomic,strong) NSString *id_name; @property (n
在我的应用程序中,我进入 CoreData 并获取一个类型为 Double 的条目,然后我试图将该值放入我的应用程序的文本字段中。 这看起来像 lengthTextField.text = lastS
用户输入一个数字( float 或整数),并且它必须大于下限。 这是从 UITextField 获取数字的代码: NSNumberFormatter * f = [[NSNumberFormatter
我正在为 iPhone 应用程序使用 NSNumber,并看看我能用它做什么。对于我的大多数变量,我简单地将它们存储为“int”或“float”或其他类型。但是,当我必须传递一个对象(例如在字典中)时
我使用 [NSNumber numberWithDouble:] 方法来形成我的 NSNumber。但是,由于某些值可能很大,我想知道是否有办法告诉 NSNumber 用科学记数法表示自己。提前致谢!
一个 NSNumber 可以存储不同的基本类型,如short、int、long、long long、float、double 但是当我这样做时,尺寸会改变 @(long long) 相比 @(int)
我编写了一个 Objective-C 类,并且在我的 iPhone 项目中的多个 View 中使用它的共享实例。它的成员变量包括 bools、ints、NSStrings 和一个 NSNumber。共
创建一个 ARC 新项目并将此代码注入(inject)到 didFinishLaunchingWithOptions 中。 for (int i=0; i < 1000000; i++) {
我对 Objective C 比较陌生,需要一些数组帮助。 我有一个 plist,其中包含一个字典和一个 NSNumber 数组,还有更多数组可供使用稍后添加。 NSMutableDictionary
我正在尝试将 NSNumber 放入数组中: NSNumber *n = [NSNumber numberWithInt:1]; [[array objectAtIndex:0] setValue:n
我正在开发一个简单的计算器 iPhone 应用程序。只是为了练习的目的。我有一个 IBAction 方法来存储用户输入的数字。整个概念是计算器应用程序堆积待处理的操作,以便用户可以执行多个操作,并且屏
我是一名优秀的程序员,十分优秀!