SetStrokeColorWithColor 中的 UIColor 触发 EXC_BAD_ACCESS - 内存问题?
我有一条大线要画,颜色会沿着线变化。
我时不时地在代码的第四行得到 EXC_BAD_ACCESS 。
我怀疑这与 *tempColor 的自动释放有关,但无法弄清楚如何让它高效工作而不崩溃。
有什么想法吗?假设运行此代码的 50 次中有 1 次会崩溃。
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, lastx, lasty);
CGContextAddLineToPoint(ctx, point.x, point.y);
UIColor *tempColor = [self colorForHex:[[heightLocal objectAtIndex:idx] doubleValue]];
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextStrokePath(ctx);
lastx = point.x;
lasty = point.y;
编辑:
在该建议之后刚刚进行了快速播放,可能认为这是因为 heightLocal 未初始化?
我将其更改为此处的代码...
if(idx > [heightLocal count]){
heightVar = 0;
NSLog(@"Made it here");
}else {
heightVar = [[heightLocal objectAtIndex:idx] doubleValue];
}
UIColor *tempColor = [self colorForHex:heightVar];
并且在第一行出现相同的错误。如果!
heightLocal 初始化使用...
NSArray *heightLocal = routeGrabInstance.pointHeights;
I have a large line to draw that the colour changes along the line.
Now and again I get EXC_BAD_ACCESS on the 4th line of the code there.
I suspect that it is something to do with the autoreleasing of the *tempColor but cannot work out how to get this to work effeiciently and not to crash.
Any ideas? This crashes on say 1 in 50 runs of this code.
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, lastx, lasty);
CGContextAddLineToPoint(ctx, point.x, point.y);
UIColor *tempColor = [self colorForHex:[[heightLocal objectAtIndex:idx] doubleValue]];
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextStrokePath(ctx);
lastx = point.x;
lasty = point.y;
EDIT:
Just had a quick play after that suggestion and possibly think it is because heightLocal is not initialized?
I cahnged it to the code here...
if(idx > [heightLocal count]){
heightVar = 0;
NSLog(@"Made it here");
}else {
heightVar = [[heightLocal objectAtIndex:idx] doubleValue];
}
UIColor *tempColor = [self colorForHex:heightVar];
and it gets the same error on the first line. The if!
heightLocal is initialized using...
NSArray *heightLocal = routeGrabInstance.pointHeights;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说的是,你得到的数组:
没有被正确保留。如果它像许多类一样,它可以作为自动释放返回。
尝试:
在你之后
I would say that the array you get with:
Is not being retained properly. If it's like many classes it could be returned as autorelease.
Try a :
after your