gpt4 book ai didi

iphone - CGMutablePathRef 中的巨大内存泄漏

转载 作者:行者123 更新时间:2023-12-03 19:07:16 27 4
gpt4 key购买 nike

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

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

提前致谢

最佳答案

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

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

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

关于iphone - CGMutablePathRef 中的巨大内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4172247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com