gpt4 book ai didi

ios - 请帮我找出这个 Objective-C 方法 (iOS) 中的内存泄漏

转载 作者:行者123 更新时间:2023-11-28 18:34:31 25 4
gpt4 key购买 nike

好吧,我真的很想找到这个。我是 Objective-C 的新手,在指针管理方面没有足够的经验。我知道这里有一个重要的内存泄漏,因为多次执行此代码会导致一堆内存警告,但找不到它在哪里或如何解决它。

下面的方法从固定数组中获取一些数据并从中创建一组图像。它正在更新固定数量的图像,但只有前“n”个计数,所以其余的都用透明的小图像填充(以清除之前的图像)。

(我怀疑每次执行时创建的 UIImages 是因为执行期间内存消耗太快。)

我已经尝试添加到行中

UIImage* img = [UIImage imageWithCGImage:image];

自动释放,因为我怀疑每次调用此代码时都会创建图像,并且我认为当 routeView[indexAux].image 指向新创建的图像时应该调用自动释放。我认为这可行,但最终以“消息发送到已释放实例”错误结束。

还尝试用以下内容替换该部分:

UIImage* img = [UIImage imageWithCGImage:image];
routeView[indexAux].image = [img copy];
[img release];

...因此已故 img 变量的唯一现有副本存在于 routeView[i].image 中,并最终被下一次执行覆盖。但是结束了同样的内存泄漏。欢迎任何帮助或提示。提前致谢!

-(void) update:(NSArray*) arr{
int indexAux = 0;
for(NSNumber* i in arr){
int index = [i integerValue];
CGContextRef context = CGBitmapContextCreate(nil,
routeView[indexAux].frame.size.width,
routeView[indexAux].frame.size.height,
8,
4 * routeView[indexAux].frame.size.width,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast);

self.lineColor = [coloresDeSeccion[index] copy];

CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 5.0);

for(int i = sections[index]; i <= sections[index + 1]; i++) {
CLLocation* location = [routes objectAtIndex:i];
CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView[indexAux]];
if(i == sections[index]) { //¿Que hacías aquí?
CGContextMoveToPoint(context, point.x, routeView[indexAux].frame.size.height - point.y);
} else {
CGContextAddLineToPoint(context, point.x, routeView[indexAux].frame.size.height - point.y);
}
}

CGContextStrokePath(context);

CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];

routeView[indexAux].image = img;

CGContextRelease(context);
indexAux++;
}

for(int i = indexAux; i < 15; i++){
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
routeView[indexAux].image = blank;
}
}

最佳答案

你在这里创建一个图像:

    CGImageRef image = CGBitmapContextCreateImage(context);

这个函数中包含单词Create。根据Create Rule ,你必须释放它:

CFRelease(image);

关于ios - 请帮我找出这个 Objective-C 方法 (iOS) 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713952/

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