Ser Pounce
Ser Pounce

Reputation: 14571

Memory Leak When Handling CGMutablePath

I have a memory leak when working with a CGMutablePath under the following circumstances:

- (CGMutablePathRef) textMutablePathForFrame:(CGRect)frame
{
    CGAffineTransform transform = CGAffineTransformMakeScale(frame.size.width / self.shapeMutablePathSize.width, frame.size.height / self.shapeMutablePathSize.height);

    return CGPathCreateMutableCopyByTransformingPath(self.shapeMutablePath, &transform);
}

- (CTFrameRef) textFrameForFrame:(CGRect)frame framesetter:(CTFramesetterRef)framesetter
{
    CGMutablePathRef textMutablePath = [self textMutablePathForFrame:frame];
    CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), textMutablePath, NULL);
    CGPathRelease(textMutablePath);

    return textFrame;
}

Through instrument analysis I get a memory leak at the line with "return" in textMutablePathForFrame that says "Potential leak of an object allocated on line 132" (line 132 is the return line itself).

I also get a memory leak in textFrameForFrame at the line "CGPathRelease(textMutablePath);" that says: "Incorrect decrement of the reference count of an object that is not owned at this point by the caller".

Can't wrap my head around this, felt like I was finally getting a good understanding of memory management in Core.

UPDATE: Looking like this is possibly a bug, going to bump it one more time to see if anyone else feels differently.

Upvotes: 0

Views: 704

Answers (2)

Rob Napier
Rob Napier

Reputation: 299643

@JonathanCichon is correct in principle, but has the wrong naming convention. The correct naming convention for the ObjC method is newMutablePathForFrame. The analyzer is correct. The "Create" rule only applies to Core Foundation. The ObjC naming conventions are in the Advanced Memory Management Programming Guide. The Core Foundation rules, which are slightly different and include the "Create" rule, are in the Memory Management Programming Guide for Core Foundation.

Upvotes: 2

Jonathan Cichon
Jonathan Cichon

Reputation: 4406

I dont think you have a memory leak, change your textMutablePathForFrame method-name to createMutablePathForFrame and the warnings should disapear.

Upvotes: 1

Related Questions