【问题标题】:Memory leak for CoreImageCoreImage 的内存泄漏
【发布时间】:2014-06-30 23:33:21
【问题描述】:

我创建了如下模糊图像:

//Blur the UIImage
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 2] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
CIImage *finalImage = [resultImage imageByCroppingToRect:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height)];

//create UIImage from filtered image
UIImage *blurrredImage = [[UIImage alloc] initWithCIImage:finalImage];

CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"];
[colorMatrixFilter setDefaults];
[colorMatrixFilter setValue:finalImage forKey:kCIInputImageKey];
[colorMatrixFilter setValue:[CIVector vectorWithX:0.25 Y:0 Z:0 W:0] forKey:@"inputRVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0.35 Z:0 W:0] forKey:@"inputGVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0.4 W:0] forKey:@"inputBVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"];

// Get the output image recipe
CIImage *outputImage = [colorMatrixFilter outputImage];

// Create the context and instruct CoreImage to draw the output image recipe into a CGImage
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
blurrredImage = [UIImage imageWithCGImage:cgimg];


CGImageRelease(cgimg);

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = blurrredImage;

//insert blur UIImageView below transparent view inside the blur image container
[blurContainerView insertSubview:newView belowSubview:transparentView];

我已经发布了 CGImageRef,但仍然出现内存泄漏

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

我还提到了所有其他相关问题,并实施提供的答案仍然会出现内存泄漏

使用 @autoreleasepool { } 并尝试将对象设置为 nil

context = nil;
outputImage = nil;
gaussianBlurFilter = nil;
viewImage = nil;

之前

CGImageRelease(cgimg);

仍然显示内存泄漏,描述如下。 ARC 开启,ios 版本为 6.1 和 7

责任库是:CoreImage 负责任的调用者是:CI::GLESContext::program_for_name(__CFString const*)

谁能建议我如何解决这个内存泄漏?

提前感谢您的帮助。

【问题讨论】:

  • 你是否也得到了其他泄露的对象,还是只有 CGImage 对象?
  • 获取 CoreImage 和 main.m 中的内存泄漏 @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([OpinoAppDelegate class])); }
  • 其他泄露的对象是什么类型?图像?图像视图?还是只泄露了 CGImageRef 对象?
  • 只有 CGImageRef 对象被泄露可能是因为 Context Object
  • 这有点奇怪。您创建的唯一 CGImageRef 似乎可以正确发布。 CIContext 是一个 NSObject,应该由 ARC 处理。

标签: ios memory-leaks core-image


【解决方案1】:

所有核心框架都使用 C 语言,而不是 Objective-C。您必须手动进行内存管理,并且 ARC 不会对您有太大帮助,除非将某些(免费桥接)引用的所有权从 Core 转移到 Objective-C 对象。您真的应该阅读Apple documentation 以了解您需要做什么。

不过,简而言之,设置对 nil 的引用(在 C 中应该是 NULL)会给您留下悬空指针和内存泄漏。

【讨论】:

  • 您好,感谢您的快速回复,我会检查文档。
【解决方案2】:

问题是不断调用

[CIContext contextWithOptions:nil]

我建议在您的班级中创建CIContext 的属性:

@property (retain, nonatomic) CIContext *context;

只有当你的上下文为 nil 时才实例化它

if( self.context == nil ) {
    self.context = [CIContext contextWithOptions:nil];
}

【讨论】:

  • 我不确定这是否是解决方案。即使使用属性,我也有泄漏。我认为一般的答案是使用@autoreleasepool、CG对象Release等。
猜你喜欢
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2010-12-17
  • 2016-05-09
相关资源
最近更新 更多