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
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":
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With