CGMutablePathRef中的巨大内存泄漏

时间:2010-11-13 11:17:49

标签: iphone core-foundation cgpath

我在地图中渲染了近1000个多边形。我使用

获取多边形的路径
-   (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);
    }
}
}

我得到了泄密

CGPathRelease(interiorPath);

return path;

我知道我必须使用CGPathRelease释放路径,但在必须返回的时候将其释放。

两者都泄漏了巨大的记忆。 我已经做了好几天了,请帮忙。

先谢谢

3 个答案:

答案 0 :(得分:7)

您应该将方法重命名为-createPolyPath:以明确表示它正在返回需要发布的Core Foundation对象,然后在您调用-createPolyPath:的代码中,您需要像这样释放它:

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

请参阅"Memory Management Programming Guide for Core Foundation"

答案 1 :(得分:1)

我认为您必须从new开始重命名您的方法,例如newPolyPath...。我会做到这一点,它现在对我来说没有更多的泄漏......

每次使用路径后,您还必须使用CGPathRelease(path);

答案 2 :(得分:1)

尝试使用CGPathRelease(path);

例如:

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

这里有很棒的提示:

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