开发者

UIColor in SetStrokeColorWithColor triggers EXC_BAD_ACCESS - memory issue?

开发者 https://www.devze.com 2023-01-23 19:17 出处:网络
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 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:

J开发者_如何学JAVAust 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;


I would say that the array you get with:

routeGrabInstance.pointHeights;

Is not being retained properly. If it's like many classes it could be returned as autorelease.

Try a :

[heightLocal retain]

after your

NSArray *heightLocal = routeGrabInstance.pointHeights;
0

精彩评论

暂无评论...
验证码 换一张
取 消