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

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

问题描述

喜欢这篇文章:

  • iPhone - UIImage Leak, ObjectAlloc Building

我遇到了类似的问题。从不释放来自 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 上的retainCount和 _imageView 都是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天全站免登陆