代码之家  ›  专栏  ›  技术社区  ›  ipraba

CGmutablePathRef内存泄漏严重

  •  4
  • ipraba  · 技术社区  · 14 年前

    我在地图上渲染了近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来释放path,但是当我必须返回时在哪里释放它。

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

    提前谢谢

    3 回复  |  直到 10 年前
        1
  •  7
  •   Baby Groot Duleendra    11 年前

    您应该将方法重命名为 -createPolyPath: 为了清楚地表明它正在返回一个需要释放的核心基础对象,然后在您调用的代码中 -创建多路径: ,您需要这样释放它:

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

    查看 "Memory Management Programming Guide for Core Foundation" :

        2
  •  1
  •   bluish dmajkic    12 年前

    我认为您必须从 new 喜欢 newPolyPath... . 我会这样做的,现在对我来说这是工作,不会再有泄漏了…

    您还必须使用 CGPathRelease(path); 每次使用你的路径之后。

        3
  •  1
  •   Adam Cooper    10 年前

    尝试使用cgpathrelease(path);

    例如:

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

    这里有一个很好的提示:

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