Huge memory leaks in CGMutablePathRef

I selected almost 1000 polygons on the map. I get the polygon path using

- (CGPathRef)polyPath:(MKPolygon *)polygon { MKMapPoint *points = [polygon points]; NSUInteger pointCount = [polygon pointCount]; NSUInteger i; if (pointCount < 3) return NULL; CGMutablePathRef path = CGPathCreateMutable(); if([polygon isKindOfClass:[MKPolygon class]]) { for (MKPolygon *interiorPolygon in polygon.interiorPolygons) { CGPathRef interiorPath = [self polyPath:interiorPolygon]; CGPathAddPath(path, NULL, interiorPath); CGPathRelease(interiorPath); } } CGPoint relativePoint = [self pointForMapPoint:points[0]]; CGPathMoveToPoint(path, NULL, relativePoint.x, relativePoint.y); for (i = 1; i < pointCount; i++) { relativePoint = [self pointForMapPoint:points[i]]; CGPathAddLineToPoint(path, NULL, relativePoint.x, relativePoint.y); } return path; } - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context { MultiPolygon *multiPolygon = (MultiPolygon *)self.overlay; for (MKPolygon *polygon in multiPolygon.polygons) { if([polygon isKindOfClass:[MKPolygon class]]) { CGPathRef path = [self polyPath:polygon]; if (path) { [self applyFillPropertiesToContext:context atZoomScale:zoomScale]; CGContextBeginPath(context); CGContextAddPath(context, path); CGContextDrawPath(context, kCGPathEOFill); [self applyStrokePropertiesToContext:context atZoomScale:zoomScale]; CGContextBeginPath(context); CGContextAddPath(context, path); CGContextSetAlpha(context,1.0); CGContextStrokePath(context); } CGPathRelease(path); } } } 

I get a leak in

 CGPathRelease(interiorPath); 

and

 return path; 

I know that I need to clear the path using CGPathRelease, but where to free it, while I have to go back.

Both leak huge memory. I have been working on this for several days, please help.

Thanks at Advance

+4
source share
3 answers

You must rename your method as -createPolyPath: to make it clear that it returns the Core Foundation object to be released, and then in the code in which you call -createPolyPath: you need to release it

 CGPathRef path = [someObjectOrClass createPolyPath:somePolygon]; // Do some stuff with the path CGPathRelease(path); 

See “Core Foundation Memory Programming Guide” :

+7
source

I think you should rename your method starting with new , like newPolyPath... I will do it, and now it works for me without leaks on the way ...

You should also use CGPathRelease(path); after every use of your way.

+1
source

Try using CGPathRelease (path);

For instance:

 CGMutablePathRef path = CGPathCreateMutable(); // created memory allocation CGPathCloseSubpath(path); CGPathRelease(path); // released path allocation 

Great review found here:

http://the.ichibod.com/kiji/ios-memory-management-tips/

+1
source

All Articles