GPUImage内存警告

时间:2012-12-24 14:51:39

标签: ios gpuimage

在图像上应用GPUImage过滤器时,我遇到了一个奇怪的问题。我试图在图像上应用不同的过滤器,但在应用10-15过滤器后,它会给我内存警告,然后崩溃。 这是代码:

sourcePicture = [[GPUImagePicture alloc] initWithImage:self.m_imageView.image smoothlyScaleOutput:YES];

            GPUImageBrightnessFilter *bright=[[GPUImageBrightnessFilter alloc]init];
            [bright setBrightness:0.4];
            GPUImageFilter *sepiaFilter = bright;

            [sepiaFilter prepareForImageCapture];
            [sepiaFilter forceProcessingAtSize:CGSizeMake(640.0, 480.0)]; // This is now needed to make the filter run at the smaller output size
            [sourcePicture addTarget:sepiaFilter];
            [sourcePicture processImage];
            UIImage *sep=[sepiaFilter imageFromCurrentlyProcessedOutputWithOrientation:3];

            self.m_imageView.image=sep;
            [sourcePicture removeAllTargets];

如果有人遇到同样的问题,请建议。感谢

1 个答案:

答案 0 :(得分:1)

由于您没有使用ARC,因此您可能会在几个地方泄漏内存。通过不事先分配而不释放您正在创建泄漏的值。这是一篇关于内存管理的好文章。 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

检查以确保我已注释的那些斑点被正确释放,然后再次检查,如果你要添加的15个过滤器中的每一个都有两个潜在的泄漏点,那么你正在创建30个泄漏点。

编辑:我还为您添加了两个可能的修复程序,但请确保您正确管理内存以确保其他地方没有任何问题。

//--Potentially leaking here--
sourcePicture = [[GPUImagePicture alloc] initWithImage:self.m_imageView.image smoothlyScaleOutput:YES];

//--This may be leaking--     
GPUImageBrightnessFilter *bright=[[GPUImageBrightnessFilter alloc]init];              
[bright setBrightness:0.4];

GPUImageFilter *sepiaFilter = bright; 
//--Done using bright, release it;
[bright release];                           
[sepiaFilter prepareForImageCapture];
[sepiaFilter forceProcessingAtSize:CGSizeMake(640.0, 480.0)]; // This is now needed to make the filter run at the smaller output size
[sourcePicture addTarget:sepiaFilter];
[sourcePicture processImage];
UIImage *sep=[sepiaFilter imageFromCurrentlyProcessedOutputWithOrientation:3];

self.m_imageView.image=sep;
[sourcePicture removeAllTargets];
//--potential fix, release sourcePicture if we're done --
[sourcePicture release];