局部整数值在循环内随机变化

时间:2014-03-27 16:39:42

标签: ios objective-c xcode xcode5

我有一个函数可以对NSMutableArray进行一些数组操作。不知何故,在while函数的几个循环之后,2个局部变量中的值是总垃圾。它们不会在任何地方被分配或操纵。以下是:

enter image description here

这里的功能是:

-(void) normalize_path:(NSMutableArray *)path tool:(Tool)tool
{
    NSUInteger last_accepted_point_index = 0;
    NSUInteger current_point_index = 1;

    while(current_point_index < [path count]){

        VPoint * p1, * p2;
        [[path objectAtIndex:last_accepted_point_index] getValue:&p1];
        [[path objectAtIndex:current_point_index] getValue:&p2];

        //float distance = [self distance_between:p1 and:p2];

//        if(distance < MIN_POINT_DISTANCE){
//            [path removeObjectAtIndex:current_point_index];
//        }else{
//            float opacity = tool.max_opacity - distance * tool.opacity_sensitivity;
//            opacity = opacity <= tool.min_opacity ? tool.min_opacity : opacity;
//            p2->opacity = opacity;
//            float thickness = tool.max_thickness - distance * tool.thickness_sensitivity;
//            thickness = thickness <= tool.min_thickness ? tool.min_thickness : thickness;
//            p2->thickness = thickness;
//            last_accepted_point_index = current_point_index;
//            //current_point_index++;
//        }
    }
}

它只在一个地方打电话:

//...
[self normalize_path:opath tool:pen];
//...

每次运行都会创建不同的值。我很困惑!这是怎么回事?

1 个答案:

答案 0 :(得分:1)

我认为这可能是一个内存问题,您可以通过从&调用中的p1p2前面移除getValue:来解决此问题。很难完全确定,因为你没有说VPoint是什么,但通常这段代码看起来像这样:

    VPoint p1, p2;
    [[path objectAtIndex:last_accepted_point_index] getValue:&p1];
    [[path objectAtIndex:current_point_index] getValue:&p2];

然后,这会将p1p2设置为实际值。你的函数之间的距离不会引用参考,而是p1和p2的实际值(如果你想像现在一样传递引用,你将&放在p1前面,距离p2方法调用中的callBetween