IPHONE:释放对象后仍然分配内存?
我有一种使用石英将屏幕外的对象绘制到文件的方法。该方法的最后几行是:
CGRect LayerRect = CGRectMake(mX,mY, wLayer, hLayer);
CGContextDrawImage(objectContext, LayerRect, objectProxy.image.CGImage); // 1
CGRect superRect = CGRectMake(vX, vY, w,h);
CGContextDrawLayerInRect(context, superRect, objectLayer);
CGLayerRelease(objectLayer); // 2
UIImage * myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); //3
return myImage;
如您所见,在 //1 上绘制的图层在 //2 上释放,上下文在 //3 上释放。
那么,没有泄漏吧?
事实上,仪器将此报告为没有泄漏,但在运行此方法并返回主循环后,我的应用程序使用的内存比以前多了 1.38 MB。
调查仪器,在内存分配选项卡上,我看到一个条目为
Malloc 1.38 MB
overall bytes = 1.38 MB
#Overall = 1
#alloc =
Live Bytes = 1.38 MB
#Living = 1
#Transitory = 0
,该条目指向此
CGContextDrawImage(objectContext, LayerRect, objectProxy.image.CGImage); // 1
所以,显然方法内分配的内存仍然分配但没有泄漏?怎么可能呢?
如何摆脱这种内存分配以释放内存?
提前致谢!
I have a method for drawing an object offscreen to a file, using quartz. The last lines of this method are:
CGRect LayerRect = CGRectMake(mX,mY, wLayer, hLayer);
CGContextDrawImage(objectContext, LayerRect, objectProxy.image.CGImage); // 1
CGRect superRect = CGRectMake(vX, vY, w,h);
CGContextDrawLayerInRect(context, superRect, objectLayer);
CGLayerRelease(objectLayer); // 2
UIImage * myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); //3
return myImage;
as You see the layer drawn on //1 is released on //2, and the context is released on //3.
So, there's no leak right?
In fact, instruments reports this as having NO LEAKS, but after running this method and returning to the main loop, my application is using 1.38 MB of memory more than before.
Investigating on intruments, on memory allocation tab, I see an entry as
Malloc 1.38 MB
overall bytes = 1.38 MB
#Overall = 1
#alloc =
Live Bytes = 1.38 MB
#Living = 1
#Transitory = 0
and this entry points to this
CGContextDrawImage(objectContext, LayerRect, objectProxy.image.CGImage); // 1
So, apparently the memory allocated inside the method is still allocated but is not leaking?? How can that be?
How can I get rid of this memory allocation freeing the memory?
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该图像肯定会占用一些内存。我并不完全精通 iPhone 编程,但 OS X 下的图像始终是您制作图像的副本。文档说图像存在于自动释放池中,因此根据您管理池的方式,它可能会在那里存在相当长的一段时间。您可以尝试在 calling 函数中的调用周围放置一个自动释放池(将其放入上面引用的函数中将返回无效对象)。
一般来说,我可以说,一旦自动释放池开始发挥作用,尝试跟踪对象的释放将变得相当麻烦(有时甚至是不可能的......自动释放对象背后的想法是系统最知道何时释放它们(这是让像我这样的 C++ 程序员发疯的东西......但是当然 Objective C 和 Cocoa 并不是为了让我高兴:-)))
但是,假设你上面的函数被称为 drawOffline 你应该能够摆脱内存在图像中
,如果您打算使用 ret ptr 更长时间,您应该保留它以告诉系统即使自动释放池释放它也不应该删除它。
如前所述,通常对于自动释放对象,您不必担心它们的生命周期,但如果您打算将它们保留更长的时间(特别是将其存储在对象成员中以供以后使用),您需要自己保留/释放它,因为否则系统可能会把它拉到你的脚下。
此 [链接][1] 描述了 OS X 下的内存管理,但也适用于 iphone。
[1]:http://developer.apple。 com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html 链接
The image would certainly use some memory. I'm not totally proficient with iPhone programming but an image under OS X is always a copy of what you made the image from. The docs say that the image lives in an autoreleasepool, so depending on how you manage the pools, it could live there for quite some time. You could try putting an autoreleasepool around the call in the calling function (putting it into te function you're quoting above would return an invalid object).
Generally I can say that as soon as autoreleasepools are coming into play, trying to track the releasing of objects will become quite cumbersome (and impossible sometimes ... the idea behind autorelease objects is that the system knows best when to release them (which is something that drives a C++ programmer like me nuts ... but of course Objective C and Cocoa is not made to make me happy :-)))
However, assuming your function above is called drawOffline you should be able to get rid of the memory in image via
going a bit further, if you intend to use the ret ptr a bit longer you should retain it to tell the system that it should not delete it even if the autorelease pool releases it.
As said, generally with autorelease objects you don`t worry about their lifetime, but if you intend to keep them longer (especially storing it in an object member for later use) you need to retain/release it yourself, because otherwise the system could pull it right under your feet.
This [link][1] describes memory management under OS X but also applies to the iphone.
[1]: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html link
显然,在苹果解决这个问题之前,没有解决方案。
apparently there's no solution for that, until Apple fix this.