代码之家  ›  专栏  ›  技术社区  ›  Lee Armstrong

uicolor in setstrokeColor with color triggers exc_bad_access-memory issue?

  •  0
  • Lee Armstrong  · 技术社区  · 15 年前

    我画了一条大线,这条线的颜色会改变。

    我不时地在代码的第四行获得exc-bad-u访问权。

    我怀疑这与自动恢复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;
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Brad    15 年前

    我想说你得到的数组是:

    routeGrabInstance.pointHeights;
    

    未正确保留。如果它和许多类一样,它可以作为autorelease返回。

    尝试:

    [heightLocal retain]
    

    在你的

    NSArray *heightLocal = routeGrabInstance.pointHeights;