我的洪水填充方法因访问错误而崩溃,我找不到该错误,请问有什么想法吗?
当我启动我的应用程序时,我可以很好地绘制小物体,但是当我想使用像油漆桶一样的洪水填充来用油漆填充更大的物体时,它只会因 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/