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.
正如您看到绘制在//1上的层是在//2上发布的,而上下文是在//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.
实际上,instruments报告说没有泄漏,但是在运行此方法并返回到主循环之后,我的应用程序比以前多使用了1.38 MB内存。
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!
提前谢谢!
2 个解决方案
#1
1
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).
这幅图像肯定需要一些内存。我不完全精通iPhone编程,但是OS X下的图像总是你制作图像的拷贝。医生说,这张图片存在于一个自动上传池中,因此根据你如何管理这些池,它可能会在那里存活相当长一段时间。您可以尝试在调用函数的调用周围放置一个autoreleasepool(将它放到上面引用的te函数中会返回一个无效的对象)。
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 :-)))
一般来说,我可以说,一旦自动发布池开始发挥作用,试图跟踪对象的释放将变得非常麻烦(而且有时是不可能的……)autorelease对象背后的想法是,系统知道什么时候释放它们(这让像我这样的c++程序员抓狂……)当然,Objective C和Cocoa都不是用来让我开心的
However, assuming your function above is called drawOffline you should be able to get rid of the memory in image via
但是,假设上面的函数称为draw脱机,那么您应该能够通过以下方法删除映像中的内存
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *ret= [self drawOffline];
// do some work using ret (possibly copying it)
[pool release];
// memory should be released and the ret ptr is invalid from this point on.
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.
更进一步说,如果您想要使用ret ptr更长一点,您应该保留它,告诉系统,即使autorelease池释放它,它也不应该删除它。
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *ret= [self drawOffline];
[ret retain]; // prevent ret from being deleted when the pool releases it
// do some work using ret (possibly copying it)
[pool release];
// ret ptr will continue to live until you release it.
// more stuff
[ret release]; // image behind ret being freed
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.
如前所述,通常对于autorelease对象,您不需要担心它们的生命周期,但是如果您想让它们保持更长时间(特别是将其存储在对象成员中以供以后使用),您需要自己保留/释放它,因为否则系统可能会将其拖到您的脚下。
This [link][1] describes memory management under OS X but also applies to the iphone.
[1]描述了OS X下的内存管理,但也适用于iphone。
[1]: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html link
[1]:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html链接
#2
-1
apparently there's no solution for that, until Apple fix this.
显然,在苹果解决这个问题之前,没有解决办法。
#1
1
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).
这幅图像肯定需要一些内存。我不完全精通iPhone编程,但是OS X下的图像总是你制作图像的拷贝。医生说,这张图片存在于一个自动上传池中,因此根据你如何管理这些池,它可能会在那里存活相当长一段时间。您可以尝试在调用函数的调用周围放置一个autoreleasepool(将它放到上面引用的te函数中会返回一个无效的对象)。
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 :-)))
一般来说,我可以说,一旦自动发布池开始发挥作用,试图跟踪对象的释放将变得非常麻烦(而且有时是不可能的……)autorelease对象背后的想法是,系统知道什么时候释放它们(这让像我这样的c++程序员抓狂……)当然,Objective C和Cocoa都不是用来让我开心的
However, assuming your function above is called drawOffline you should be able to get rid of the memory in image via
但是,假设上面的函数称为draw脱机,那么您应该能够通过以下方法删除映像中的内存
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *ret= [self drawOffline];
// do some work using ret (possibly copying it)
[pool release];
// memory should be released and the ret ptr is invalid from this point on.
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.
更进一步说,如果您想要使用ret ptr更长一点,您应该保留它,告诉系统,即使autorelease池释放它,它也不应该删除它。
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *ret= [self drawOffline];
[ret retain]; // prevent ret from being deleted when the pool releases it
// do some work using ret (possibly copying it)
[pool release];
// ret ptr will continue to live until you release it.
// more stuff
[ret release]; // image behind ret being freed
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.
如前所述,通常对于autorelease对象,您不需要担心它们的生命周期,但是如果您想让它们保持更长时间(特别是将其存储在对象成员中以供以后使用),您需要自己保留/释放它,因为否则系统可能会将其拖到您的脚下。
This [link][1] describes memory management under OS X but also applies to the iphone.
[1]描述了OS X下的内存管理,但也适用于iphone。
[1]: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html link
[1]:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html链接
#2
-1
apparently there's no solution for that, until Apple fix this.
显然,在苹果解决这个问题之前,没有解决办法。