在设备上运行时出现EXC_ARM_DA_ALIGN错误
为什么这个代码在模拟器上运行并在实际设备上崩溃?
我有一个非常简单的代码,绘制一个圆圈。 代码子类UIView
并在模拟器上运行正常(iOS 5.1和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, circle );
上出现错误( EXC_BAD_ACCESS(code=EXC_ARM_DA_ALIGN,address=0x31459241)
CGContextAddPath( ctx, circle );
线。
我不知道问题是什么。 任何人都可以指出我正确的方向来解决这个问题吗?
这是因为您正在返回的CGPath
由circlePath
方法中创build的自动发布的UIBezierPath
所拥有。 当你添加path对象时, UIBezierPath
已经被释放,所以返回的指针指向无效的内存。 你可以通过返回UIBezierPath
本身来修复崩溃:
-(UIBezierPath *)circlePath { UIBezierPath *path = [UIBezierPath bezierPath]; [path addArcWithCenter:self.center radius:10.0 startAngle:0.0 endAngle:360.0 clockwise:YES]; return path; }
然后使用:
CGContextAddPath( ctx, circle.CGPath );