EXC_BAD_ACCESS code = 2用ARC递归方法
问题描述:
我需要实现一个递归函数来将图像分成多个小图像进行益智游戏。EXC_BAD_ACCESS code = 2用ARC递归方法
编辑:这里是如何ShapeObject的类的init方法看起来(目前只支持圆圈)ShapeObject
//shape currently has only a property named radius (it's a circle)
- (id)initWithShape:(Shape*)shape rotation:(float)rotation position:(CGPoint)position
{
self = [super init];
if (self) {
_position=position;
_shape=shape;
_rotation=MAX(0, MIN(rotation, 360));
_color=nil;
_shapePath=CGPathCreateMutable();
CGPathAddArc(_shapePath, NULL, _position.x, _position.y, _shape.radius, 2*M_PI, 0, YES);
CGPathCloseSubpath(_shapePath);
}
return self;
}
// in the processing class
-(void)recursiveTest:(ShapeObject*)shapeObject{
if (!CGRectIntersectsRect(CGPathGetBoundingBox(shapeObject.shapePath), contextRect)) {
return;
}
for (ShapeObject *obj in shapeObjects) {
if (ccpFuzzyEqual(obj.position, shapeObject.position, 5)) {
//break;
return; //just return
}
}
[shapeObjects addObject:shapeObjects]; //in front of method calls
[self recursiveTest:[[ShapeObject alloc]initWithShape:shapeObject.shape rotation:0 position:findPoint(shapeObject.position, 300, shapeObject.shape.radius*2)]];
[self recursiveTest:[[ShapeObject alloc]initWithShape:shapeObject.shape rotation:0 position:findPoint(shapeObject.position, 240, shapeObject.shape.radius*2)]];
[self recursiveTest:[[ShapeObject alloc]initWithShape:shapeObject.shape rotation:0 position:findPoint(shapeObject.position, 60, shapeObject.shape.radius*2)]];
[self recursiveTest:[[ShapeObject alloc]initWithShape:shapeObject.shape rotation:0 position:findPoint(shapeObject.position, 120, shapeObject.shape.radius*2)]];
[shapeObjects addObject:shapeObjects];
}
我的逻辑之后,它应该工作像这样:检查它是否超出&如果它已经添加到数组中。如果不是,则调用邻居,直到所有形状对象都在阵列中并且遍历整个图片。
我做这一切在后台线程,但我只要一启动功能我得到EXC_BAD_ACCESS代码2.
看看周围后,我发现,码2是一些与指针有关。
显然问题发生在我内部创建路径的地方,但我不明白为什么它应该,因为没有指针,只是一个简单的CreateMutablePath,从形状,位置和旋转,并关闭实际路径路径。而已。
此外,它不是内存泄漏,我在模拟器上测试我的mac,我有足够的内存可用于所有可能的对象。问题在别的地方。
答
从堆栈跟踪中可以清楚地看到,对于堆栈而言,递归太深。尽管有很多RAM可用,但堆栈是有限的。最大堆栈大小有点不清楚,但我认为它可能不超过1MB。
我一定会喜欢看'ShapeObject'的定义。如何在那里存储'路径'? – Sulthan 2013-05-01 15:27:22
就这样我们在这里清楚了:'没有指针,只有一个简单的CreateMutablePath''' CGPathRef'和其他这样的Core Foundation对象也是指针。例如。 'CGPathRef'被定义为'typedef const struct CGPath * CGPathRef;' – Mar0ux 2013-05-01 15:28:49
@Sulthan以及它们全部属性 – skytz 2013-05-01 15:37:13