the_critic
the_critic

Reputation: 12820

Releasing CGMutablePathRef is problematic

I get hexTouchAreas as the return value of my method -drawHexagonTouchArea, but when i analyze my project, the line where it says CGMutablePathRef hexTouchArea = CGPathCreateMutable(); produces a warning : "Value stored to 'hexTouchArea' during its initialization is never read". And at the line which says CGPathRelease(hexTouchArea); it complains that I do not own that object, and that it is superfluous to release it.

Another warning is at line return path; where the Analyzer says : "Potential leak of an object allocated on line 629 and stored into path (which is CGMutablePathRef path = CGPathCreateMutable();)"

-(void)createTouchAreas{

    int realPositionsCount =[[GameStateSingleton sharedMySingleton]getSharedRealCountOfPositions]; 
    hexTouchAreas = [[GameStateSingleton sharedMySingleton]getSharedHexTouchAreas];
    NSMutableDictionary *existingHexagons = [[GameStateSingleton sharedMySingleton]getExistingHexagons];
    for (int i = 0; i <= realPositionsCount;i++){
        //create touchareas
        NSString *hexKey = [NSString stringWithFormat:@"hexagon%d", i];
        CCSprite *theSprite = [[existingHexagons objectForKey:hexKey]objectForKey:@"realSprite"];
        CGPoint touchAreaOrigin = ccp(theSprite.position.x -22, theSprite.position.y-40);
        NSString *touchAreaKey = [NSString stringWithFormat:@"hexTouchArea%d",i];
        CGMutablePathRef hexTouchArea = CGPathCreateMutable();
        hexTouchArea = (CGMutablePathRef) [self drawHexagonTouchArea:touchAreaOrigin];
        [hexTouchAreas setObject:(id)hexTouchArea forKey:touchAreaKey];
        [[GameStateSingleton sharedMySingleton]setSharedHexTouchAreas:hexTouchAreas];
        CGPathRelease(hexTouchArea);

    }



}

method creating and returning the path:

-(CGMutablePathRef)drawHexagonTouchArea:(CGPoint)origin
{   


    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint newloc = CGPointMake(origin.x, origin.y);

    CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
    CGPathAddLineToPoint(path, NULL, newloc.x -22,newloc.y + 38);
    CGPathAddLineToPoint(path, NULL, newloc.x + 0, newloc.y + 76);
    CGPathAddLineToPoint(path, NULL, newloc.x + 46,  newloc.y + 76);
    CGPathAddLineToPoint(path, NULL, newloc.x +66,newloc.y + 40);
    CGPathAddLineToPoint(path, NULL, newloc.x +44, newloc.y + 0);
    CGPathCloseSubpath(path);
    return path;
}

Upvotes: 0

Views: 500

Answers (1)

Ishay Tubi
Ishay Tubi

Reputation: 21

With the following code, you create a mutable path, assign it to hexTouchArea, and then overwrite hexTouchArea with another created object.

CGMutablePathRef hexTouchArea = CGPathCreateMutable();
hexTouchArea = (CGMutablePathRef) [self drawHexagonTouchArea:touchAreaOrigin];

What you probably want to use is the following.

CGMutablePathRef hexTouchArea = [self drawHexagonTouchArea:touchAreaOrigin];

Upvotes: 1

Related Questions