EXC_BAD_ACCESS code=2 in recursive method with ARC

I need to implement a recursive function to split an image into multiple small images for a puzzle game.

edit : Here is how ShapeObject's class init method looks (currently only supporting circles) //init method of 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];

}

The stack trace:

After my logic it should work like this: check if it out of bounds & if it's already added in array. If not then call neighbors until all shape objects are in the array and whole picture is traversed.

I do all this in a background thread but as soon as i start the function i get EXC_BAD_ACCESS code 2.

After a look around i found out that code 2 is something related with pointers.

Apparently the problem occurs where i create the path internally, but i don't get why it should, because there is no pointer there, just a simple CreateMutablePath, make the actual path from shape ,position and rotation, and close the path. That's it.

Also, its not a memory leak, i'm testing on my mac in simulator and i have enough memory free for all the possible objects. The problem is somewhere else.

Answers


It's clear from the stack trace that your recursion is too deep for the stack. Even though there is a lot of RAM available the stack is limited. The maximum stack size is a little unclear but I think it may be no more than 1MB.


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.