另一个 iPhone - CGBitmapContextCreateImage 泄漏 [英] Another iPhone - CGBitmapContextCreateImage Leak

查看:28
本文介绍了另一个 iPhone - CGBitmapContextCreateImage 泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜欢这篇文章:

我遇到了类似的问题.create_bitmap_data_provider 中 malloc 的指针永远不会被释放.我已经验证相关的图像对象最终会被释放,而不是提供者的分配.我应该明确创建一个数据提供者并以某种方式管理它的内存吗?看起来像一个黑客.

I'm having a similar problem. The pointer from the malloc in create_bitmap_data_provider is never freed. I've verified that the associated image object is eventually released, just not the provider's allocation. Should I explicitly create a data provider and somehow manage it's memory? Seems like a hack.

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, blah blah blah);
CGColorSpaceRelease(colorSpace);

// ... draw into context

CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];

CGImageRelease(imageRef);
CGContextRelease(context);

<小时>

在下面 fbrereto 的回答之后,我把代码改成了这样:


After fbrereto's answer below, I changed the code to this:

- (UIImage *)modifiedImage {
    CGSize size = CGSizeMake(width, height);

    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // draw into context   

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;  // image retainCount = 1
}    

// caller: 
{
    UIImage * image = [self modifiedImage]; 
    _imageView.image = image; // image retainCount = 2
}

// after caller done, image retainCount = 1, autoreleased object lost its scope

不幸的是,这仍然存在与水平翻转图像的副作用相同的问题.它似乎在内部对 CGBitmapContextCreateImage 做同样的事情.

Unfortunately, this still exhibits the same issue with a side effect of flipping the image horizontally. It appears to do the same thing with CGBitmapContextCreateImage internally.

我已验证我的对象的 dealloc 已被调用.在我释放 _imageView 之前,_imageView.image_imageView 上的 retainCount 都是 1.这真的没有道理.其他人似乎也有这个问题,我是最后一个怀疑 SDK 的人,但这里是否有 iPhone SDK 错误???

I have verified my object's dealloc is called. The retainCount on the _imageView.image and the _imageView are both 1 before I release the _imageView. This really doesn't make sense. Others seem to have this issue as well, I'm the last one to suspect the SDK, but could there be an iPhone SDK bug here???

推荐答案

看起来问题是你释放了一个指向返回的 CGImage 的指针,而不是 CGImage代码>本身.我之前也遇到过类似的问题,分配不断增长,最终应用程序崩溃.我通过分配 CGImage 而不是 CGImageRef 解决了这个问题.更改后,在 Insturments 中使用分配运行您的代码,您应该不会再看到 malloc 的永久内存消耗.同样,如果您使用类方法 imageWithCGImage,您将不必担心稍后自动释放您的 UIImage.

It looks like the problem is that you are releasing a pointer to the returned CGImage, rather than the CGImage itself. I too was having similar issues before with continual growing allocations and an eventual app crash. I addressed it by allocating a CGImage rather than a CGImageRef. After the changes run your code in Insturments with allocations, and you should not see anymore perpetual memory consumption from malloc. As well if you use the class method imageWithCGImage you will not have to worry about autoreleasing your UIImage later on.

我是在 PC 上输入的,所以如果你把它直接放到 XCode 中,你可能会遇到语法问题,我提前道歉;但是校长是健全的.

I typed this on a PC so if you drop it right into XCode you may have syntax issue, I appologize in advance; however the principal is sound.

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = CGBitmapContextCreate(NULL, blah blah blah); 
CGColorSpaceRelease(colorSpace);

// ... draw into context  

CGImage cgImage = CGBitmapContextCreateImage(context); 
UIImage * image = [UIImage imageWithCGImage:cgImage];  
CGImageRelease(cgImage); 
CGContextRelease(context);
return image;

这篇关于另一个 iPhone - CGBitmapContextCreateImage 泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆