I am trying to dynamically draw curves along a path in an attempt to represent mountains.
I have a function that returns a CGPathRef
, which is a C pointer to a struct.
-(CGPathRef)newPath
{
CGMutablePathRef mutablePath = CGPathCreateMutable();
//inserting quad curves, etc
return mutablePath;
}
I then pass these CGPathRefs
around by wrapping them in a UIBezierPath
.
-(NSArray*)otherFunction
{
CGPathRef ref = [self newPath];
UIBezierPath *path = [UIBezierPath bezierPathWithCGPath: ref];
NSArray* paths = @[path];
CGPathRelease(ref);
return paths;
}
I then take the returned array of paths and display them to the screen using an SKShapeNode
.
SKShapeNode *node = [SKShapeNode new];
NSArray* paths = [self otherFunction];
CGPathRef ref = [[paths firstObject] CGPath];
node.path = ref;
node.fillColor = [UIColor orangeColor];
node.lineWidth = 2;
And finally.
[self addChild:node];
CGPathRelease(node.path);
After I repeat this sequence of action a few times my program breaks and shows me.
UIApplicationMain with EXC_BAD_ACCESS code = 2.
I understand there is a memory leak.
My question is how do I handle releasing the CGPathRef
when I end up passing it through a few functions and wrapping it in another class?
I updated the code and am now receiving a EXC_I386_GPFLT error.