在设备上运行时出现EXC_ARM_DA_ALIGN错误 [英] EXC_ARM_DA_ALIGN error when running on a device
问题描述
为什么这个代码在模拟器上运行并在真实设备上崩溃?
Why is this code running on the simulator and crashing on a real device?
我有一个非常简单的代码,它绘制了一个圆圈。代码子类 UIView
并在Simulator上运行正常(适用于iOS 5.1和iOS 6.0)。
I have a very simple code that draws a circle. The code subclasses UIView
and runs fine on the Simulator (both for iOS 5.1 and iOS 6.0).
Circle.h
#import <UIKit/UIKit.h>
@interface Circle : UIView
@end
Circle.m
#import "Circle.h"
@implementation Circle
-(CGPathRef) circlePath{
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:self.center radius:10.0 startAngle:0.0 endAngle:360.0 clockwise:YES];
return path.CGPath;
}
- (void)drawRect:(CGRect)rect
{
CGPathRef circle = [self circlePath];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddPath( ctx, circle );
CGContextStrokePath(ctx);
}
@end
当我尝试执行运行iOS 5.1.1的iPad2上的代码我在 CGContextAddPath(ctx上)收到错误(
line。 EXC_BAD_ACCESS(代码= EXC_ARM_DA_ALIGN,地址= 0x31459241)
) circle);
When I try to execute the code on an iPad2 running iOS 5.1.1 I get an error ( EXC_BAD_ACCESS(code=EXC_ARM_DA_ALIGN,address=0x31459241)
) on the CGContextAddPath( ctx, circle );
line.
我不知道问题是什么。任何人都可以指出我正确的方向来解决这个问题吗?
I have not a clue of what the problem is. Can anyone point me in the right direction to solve this issue?
推荐答案
这是因为 CGPath
您要返回的是由 circlePath
方法中创建的自动释放 UIBezierPath
所有。当您添加路径对象时, UIBezierPath
已被释放,因此返回的指针指向无效的内存。您可以通过返回 UIBezierPath
本身来修复崩溃:
This is because the CGPath
you are returning is owned by the autoreleased UIBezierPath
created in the circlePath
method. By the time you are adding the path object the UIBezierPath
has been released, so the returned pointer is pointing to invalid memory. You can fix the crash by returning the UIBezierPath
itself:
-(UIBezierPath *)circlePath {
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:self.center radius:10.0 startAngle:0.0 endAngle:360.0 clockwise:YES];
return path;
}
然后使用以下方式绘制:
Then draw using:
CGContextAddPath( ctx, circle.CGPath );
这篇关于在设备上运行时出现EXC_ARM_DA_ALIGN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!