【问题标题】:Huge Memory Leak in CGMutablePathRefCGMutablePathRef 中的巨大内存泄漏
【发布时间】:2010-11-13 11:17:49
【问题描述】:

我已经在地图中渲染了近 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 释放路径,但是当我必须返回时在哪里释放它。

两者都泄漏了巨大的内存。 我已经为此工作了几天,请帮助。

提前致谢

【问题讨论】:

    标签: iphone core-foundation cgpath


    【解决方案1】:

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

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

    "Memory Management Programming Guide for Core Foundation"

    【讨论】:

    • 为了清楚起见,您必须在调用-createPolyPath: 的每个点之后添加CGPathRelease(),而不仅仅是从内部调用它的时间。
    • 抱歉,我没弄好。我在 -createPolyPath: 调用之后给出了 CGPathRelease()。仍然泄漏。
    • 我认为 obj-c 方法的约定是“newX”而不是“createX”?
    【解决方案2】:

    我认为您必须重命名以new 开头的方法,例如newPolyPath...。我会做到的,它现在对我有用,路径上不再有泄漏......

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

    【讨论】:

    • 重命名方法不会影响是否有一个like,它只会影响Xcode的静态分析器认为是泄漏的地方。每次调用 CGPathCreate 时都必须调用 CGPathRelease,否则会发生泄漏。
    【解决方案3】:

    尝试使用 CGPathRelease(path);

    例如:

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

    在这里找到很棒的提示:

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

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2014-02-10
      • 2013-05-15
      • 1970-01-01
      相关资源
      最近更新 更多