2010-11-13 133 views
4

我在地圖中渲染了近1000個多邊形。我得到使用巨大的CGMutablePathRef中的內存泄漏

- (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釋放路徑,但在那裏將其釋放,而我必須回來。

兩者都泄漏了巨大的記憶。 我一直在爲此工作數日,請幫忙。

由於提前

回答

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"

+0

謝謝您的回答Nick..I上它的工作。但它仍然泄漏.. – iPrabu 2010-11-13 12:35:28

+0

只要你清楚地說,你必須在你調用'-createPolyPath:'的每一個點之後添加'CGPathRelease()',而不僅僅是你自己調用它的時間。 – 2010-11-13 13:01:32

+0

對不起,人不能正確。我在-createPolyPath:called後給了CGPathRelease()。仍然泄漏。 – iPrabu 2010-11-13 13:32:31

1

我認爲你必須重命名你的方法開始newnewPolyPath... 。我會做到這一點,現在對我來說,沒有更多的路徑泄漏...

你也必須使用CGPathRelease(path);每次使用後的路徑。

+0

重命名該方法不會影響是否存在類似問題,它僅影響Xcode的靜態分析器認爲是泄漏的內容。每次調用CGPathCreate時都必須調用CGPathRelease,否則將發生泄漏。 – 2011-07-31 10:45:08

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/