Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge Memory Leak in CGMutablePathRef

I Have Rendered nearly 1000 Polygons in the map. I get the path of the polygon 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 leak in

CGPathRelease(interiorPath);

and

return path;

I know that i have to release path using CGPathRelease but where to release it while i have to return.

Both leaks a huge memory. I have been working on this for days, Please help.

Thanks in Advance

like image 768
ipraba Avatar asked Dec 29 '22 05:12

ipraba


2 Answers

You should rename your method to be -createPolyPath: to make it clear that it is returning a Core Foundation object that needs to be released, then in the code in which you call -createPolyPath:, you need to release it like so:

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

See the "Memory Management Programming Guide for Core Foundation":

like image 114
Nick Forge Avatar answered Jan 10 '23 22:01

Nick Forge


I think that you must rename your method starting with new like newPolyPath.... I'll done that and it's work now for me with no more leaks on path...

You also must use CGPathRelease(path); after each use of your path.

like image 35
Yannick Avatar answered Jan 10 '23 20:01

Yannick