Pep*_*per 0 memory-leaks memory-management objective-c uiimage ios
好的,我真的很想找到这个.我是Objective-C的新手,并且没有足够的指针管理经验.我知道这里有一个重要的内存泄漏,因为多次执行此代码会导致一堆内存警告,但找不到它或如何解决它.
下面的方法从固定数组中获取一些数据并从中创建一组图像.它正在更新固定数量的图像,但只有第一个'n'计数,所以其余部分都填充了透明的小图像(以清除之前的图像).
(我怀疑每次执行都会创建UIImages,因为执行期间内存消耗过快.)
我已经尝试添加到该行
UIImage* img = [UIImage imageWithCGImage:image];
Run Code Online (Sandbox Code Playgroud)
自动释放,因为我怀疑每次调用此代码时都会创建图像,并且我认为当routeView [indexAux] .image指向新创建的图像时应该调用自动释放.我认为这可行,但它最终以"发送到解除分配的实例的消息"错误结束.
还尝试用以下代码替换该部分:
UIImage* img = [UIImage imageWithCGImage:image];
routeView[indexAux].image = [img copy];
[img release];
Run Code Online (Sandbox Code Playgroud)
...因此,已故的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;
}
}
Run Code Online (Sandbox Code Playgroud)
你在这里创建一个图像:
CGImageRef image = CGBitmapContextCreateImage(context);
Run Code Online (Sandbox Code Playgroud)
此功能包含其中的单词Create
.根据创建规则,您必须释放它:
CFRelease(image);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
115 次 |
最近记录: |