xcode CGImage 内存泄漏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2341724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Memory leak in CGImage
提问by XelharK
I've got a memory leak i just don't know how to solve.
我有内存泄漏,我只是不知道如何解决。
This is the leaking code:
这是泄漏的代码:
[newImg release];
CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(Data, width,
height,
8, 4*width,
d_colorSpace,
kCGImageAlphaNoneSkipFirst);
UIGraphicsPushContext(context);
CGImageRef new_img = CGBitmapContextCreateImage(context);
UIImage * convertedImage = [[UIImage alloc] initWithCGImage:
new_img];
CGImageRelease(new_img);
CGContextRelease(context);
CGColorSpaceRelease(d_colorSpace);
newImg = convertedImage;
I modify pixel information stored in Data, then with this method i create a UIImage back from the Data (which is as unsigned char array)
我修改存储在数据中的像素信息,然后使用此方法从数据中创建一个 UIImage(作为无符号字符数组)
The xcode instruments tells me that there are leaks in here:
xcode 工具告诉我这里有泄漏:
CGImageRef new_img = CGBitmapContextCreateImage(context);
And here:
和这里:
UIImage * convertedImage = [[UIImage alloc] initWithCGImage:
new_img];
though i release both of them :( Can somebody tell me how to solve this?
虽然我释放了他们两个:(有人能告诉我如何解决这个问题吗?
Thanks in advance ^-^
提前致谢^-^
回答by Rob Napier
Almost certainly you are failing to release newImg
somewhere else in your code. Perhaps you are not releasing it in -dealloc
. If this is true, then Instruments will flag the line of code that allocated the memory. It doesn't know when you should or shouldn't have released; it just knows when the memory was allocated and when it was freed. You'll need to audit your code for how you manage this ivar.
几乎可以肯定,您没有newImg
在代码中的其他地方发布。也许你没有在-dealloc
. 如果这是真的,那么 Instruments 将标记分配内存的代码行。它不知道你什么时候应该或不应该释放;它只知道内存何时被分配以及何时被释放。您需要审核您的代码以了解如何管理此 ivar。
That said, you are directly accessing your ivars, which is bad. This is the #1 cause of memory problems in ObjC. Use properties and accessors (except in init
and dealloc
), and these problems will tend to go away. (And convert to ARC. There is very seldom a reason to use manual retain counting anymore, particularly on iOS. Still, even with ARC, I recommend using accessors and not touching your ivars directly.)
也就是说,您正在直接访问您的 ivars,这很糟糕。这是 ObjC 中内存问题的第一大原因。使用属性和访问器(除了 ininit
和dealloc
),这些问题将趋于消失。(并转换为 ARC。很少有理由再使用手动保留计数了,尤其是在 iOS 上。不过,即使使用 ARC,我还是建议使用访问器而不是直接接触您的 ivars。)
Also: do not call your property newImg
. The prefix new
has a special meaning in ObjC (it means that the returned object should have a effective +1 retain count). This can cause you memory problems and false-positive warnings. It should have a name like convertedImage
or nextImage
.
另外:不要打电话给你的财产newImg
。这个前缀new
在 ObjC 中有特殊的意义(它意味着返回的对象应该有一个有效的 +1 保留计数)。这可能会导致您出现记忆问题和误报警告。它应该有一个像convertedImage
or的名字nextImage
。
回答by pheelicks
Edit: Original post did not seem to help, so deleting and re-posting.
编辑:原始帖子似乎没有帮助,因此删除并重新发布。
Try this instead:
试试这个:
//[newImg release]; DELETE THIS LINE
CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(Data, width,
height,
8, 4*width,
d_colorSpace,
kCGImageAlphaNoneSkipFirst);
UIGraphicsPushContext(context);
CGImageRef new_img = CGBitmapContextCreateImage(context);
newImg = [UIImage imageWithCGIImage:new_img];
CGImageRelease(new_img);
CGContextRelease(context);
CGColorSpaceRelease(d_colorSpace);
Have tested this and it does not leak on my end.
已经对此进行了测试,它在我的一端没有泄漏。
回答by Vladimir Gritsenko
CGBitmapContextCreate won't release the Data you give it. Either give it NULL (in which case the context will manage its own backing memory) or release it yourself after you release the context.
CGBitmapContextCreate 不会释放你给它的数据。要么给它 NULL(在这种情况下上下文将管理它自己的后备内存),要么在你释放上下文后自己释放它。
EDIT: hereis the relevant documentation.
编辑:这里是相关文档。
回答by FreeNickname
Seems to me that you don't release convertedImage Try to replace this line
在我看来你不释放convertedImage 尝试替换这一行
UIImage * convertedImage = [[UIImage alloc] initWithCGImage:
new_img];
with this one
有了这个
UIImage * convertedImage = [[[UIImage alloc] initWithCGImage:
new_img] autorelease];
in your code.
在你的代码中。
And if I understand you correctly (based on comments to question), you declare newImg property like so:
如果我正确理解您(基于对问题的评论),您可以像这样声明 newImg 属性:
@property (nonatomic, retain) UIImage* newImg; //or something like that
In this case the synthesized setter for this property will retain any object that will be assigned to it. And release it when the value will change. So you are not responsible for releasing it and you should remove this line:
在这种情况下,此属性的合成 setter 将保留将分配给它的任何对象。并在值发生变化时释放它。所以你不负责发布它,你应该删除这一行:
[newImg release];
I guess, the best thing you can do is to read Advanced Memory Management Programming Guide. It isn't too long and yet it provides a complete overview of main conventions used in iOS memory management.
我想,你能做的最好的事情就是阅读Advanced Memory Management Programming Guide。它不会太长,但它提供了 iOS 内存管理中使用的主要约定的完整概述。
And since you also work with CoreFoundation objects/functions, consider the Memory Management Programming Guide for Core Foundation.
由于您还使用 CoreFoundation 对象/函数,请考虑Core Foundation的内存管理编程指南。
I hope it will help :)
我希望它会有所帮助:)