我需要實現一個遞歸函數來將圖像分成多個小圖像進行益智遊戲。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,我有足夠的內存可用於所有可能的對象。問題在別的地方。
我一定會喜歡看'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