CGImageRef 内存泄漏
我在使用返回 CGImageRef 的自定义方法时遇到内存泄漏。我无法正确释放“cgImage”,因为我必须归还它。我该怎么办?
- (CGImageRef)rectRoundedImageRef:(CGRect)rect radius:(int)radius
{
CGSize contextSize = CGSizeMake(rect.size.width, rect.size.height);
CGFloat imageScale = (CGFloat)1.0;
CGFloat width = contextSize.width;
CGFloat height = contextSize.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
// Draw ...
// Get your image
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
//CGImageRelease(cgImage); //If I release cgImage the app crashes.
return cgImage;
}
I'm having a memory leak when using this custom method which returns a CGImageRef. I can't release "cgImage" properly because I have to return it. What chould I do ?
- (CGImageRef)rectRoundedImageRef:(CGRect)rect radius:(int)radius
{
CGSize contextSize = CGSizeMake(rect.size.width, rect.size.height);
CGFloat imageScale = (CGFloat)1.0;
CGFloat width = contextSize.width;
CGFloat height = contextSize.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
// Draw ...
// Get your image
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
//CGImageRelease(cgImage); //If I release cgImage the app crashes.
return cgImage;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
cgImage
归您的方法所有,您需要返回它并让调用者负责通过CFRelease
释放它。您还可以返回包装在
UIImage
实例中的CGImage
,如下所示:cgImage
is owned by your method, you need to return it and give responsibility to the caller to release it throughCFRelease
.You can also return the
CGImage
wrapped inside aUIImage
instance, like this:这是 Core Foundation 对象的普遍问题,因为 CF 中没有自动释放池。在我看来,您有两种选择来解决问题:
-newRectRoundedImageRef:radius:
之类的名称,以告诉调用者他拥有返回对象的所有权并负责释放它。[UIImage imageWithCGImage:]
)。这可能就是我会做的。This is a general problem with Core Foundation objects because there is no autorelease pool in CF. As I see it, you have two options to solve the problem:
-newRectRoundedImageRef:radius:
to tell the caller that he takes ownership of the returned object and responsible for releasing it.CGImageRef
in an autoreleasedUIImage
object and return that ([UIImage imageWithCGImage:]
). That's probably what I would do.您可以自动释放与 Core Foundation 兼容的对象。它只是看起来有点奇怪。 :)
GC 安全的方式是这样的:
在 iOS 上安全但在 Mac 上不再安全的快捷方式是这样的:
任一者都将图像放置在自动释放池中并防止泄漏。
You can autorelease a Core Foundation-compatible object. it just looks a bit wonky. :)
The GC-safe way is like so:
The shortcut, which is safe on iOS but no longer safe on the Mac, is this:
Either one will place the image in an autorelease pool and prevent a leak.
正如建议的那样,我们使用了:
但我们仍然遇到内存泄漏。
我们的解决方案是用一个
块包装代码,这解决了我们的问题。
As suggested, we used:
but we still got an memory leak.
our solution was to wrap code with an
block and that solve our problem.