iphone - 洪水填充方法因错误访问错误而崩溃有什么想法吗?

标签 iphone objective-c ios xcode ipad

我的洪水填充方法因访问错误而崩溃,我找不到该错误,请问有什么想法吗?

当我启动我的应用程序时,我可以很好地绘制小物体,但是当我想使用像油漆桶一样的洪水填充来用油漆填充更大的物体时,它只会因 BAD ACCESS 错误而崩溃。

我是一个菜鸟,我尝试了很多东西,递归的次数增加了,但它仍然崩溃。 我在这个应用程序中使用 ARC。

-(void)paintingBucket:(int)point point2:(int)point2 width:(int)width colorAtPoint:(UIColor *)color {

int offset = 0;
int x = point;
int y = point2;
if (point<1025 && point2<705) {

offset = 4*((width*round(y))+round(x));

int alpha = data[offset];
int red = data[offset + 1];
int green = data[offset + 2];
int blue = data[offset + 3];
color1 = [UIColor colorWithRed:(green/255.0f) green:(red/255.0f) blue:(alpha/255.0f) alpha:(blue/255.0f)];

   if ([color1 isEqual: color] ) {

        color3 = self.currentColor ;
        CGFloat r,g,b,a;
        [color3 getRed:&r green:&g blue: &b alpha: &a];
        int reda = (int)(255.0 * r);
        int greena = (int)(255.0 * g);
        int bluea = (int)(255.0 * b);
        int alphaa = (int)(255.0 * a);
       // NSLog(@" red: %u green: %u blue: %u alpha: %u", reda, greena, bluea, alphaa);

        data[offset + 3] = alphaa;
        data[offset + 2] = reda;
        data[offset + 1] = greena;
        data[offset] = bluea;

        [self paintingBucket:x+1 point2:y  width:width colorAtPoint:color];
        [self paintingBucket:x point2:y+1  width:width colorAtPoint:color];
        [self paintingBucket:x-1 point2:y  width:width colorAtPoint:color];
        [self paintingBucket:x point2:y-1  width:width colorAtPoint:color];

            }

        }

    }

编辑: 当它崩溃时我得到的唯一信息是“0x995d7d5f:calll 0x995d7d64; szone_malloc_should_clear + 14”

我知道存在一些内存问题,但我无法识别它,我解决了它。 正如我所说,我是 Objective C 的菜鸟,所以任何帮助都会很棒。

最佳答案

几乎可以肯定是堆栈溢出。当您递归地填充一个区域时,堆栈上的项目数等于前沿的长度,对于较大的区域,前沿可能会变得相当长。

您应该将递归方法替换为基于队列的方法来解决此问题。

关于iphone - 洪水填充方法因错误访问错误而崩溃有什么想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12247619/

相关文章:

objective-c - 更改 NSDatePicker 的默认突出显示颜色

iphone - 如何安全删除未保存的托管对象?

ios - 隐藏在 iPhone 6 键盘下的自动更正

iphone - 如何比较两个实际上等效但具有装饰性字符串差异的 NSURL?

objective-c - 在 Objective-C 中使用三元运算符有什么限制?

ios - 根据维护顺序快速解析json

ios - kSecAttrIsInvisible 在 iOS 中的相关性

iphone - 使用本地时区将 unix 时间戳转换为 NSdate

iphone - 有适用于 iPhone/iPod touch 和 iPad 的物理引擎 SDK 吗?

iPhone连续动画