Using Instruments, I keep on getting pointed to a memory leak with a UIImage.我一直在使用仪器,越来越指出,一个UIImage内存泄漏。
I think I'm assigning and releasing the memory correctly.我想我分配和释放内存正确。The leaked object in instruments is described as NSConcreteData泄漏的对象文书中被描述为NSConcreteData
Is the following the correct way to assign and release a UIImage?按照正确的方式来分配和释放一个UIImage?
UIImage* flagimg = [UIImage imageWithData: [NSData dataWithContentsOfURL:url2]]; [flagimg release]; flagimg =nil;
[UIImage imageWithData:]
returns an autoreleased object, which should not be released by you again.[UIImage imageWithData:]
返回一个自动释放的对象,它不应该被释放,你再次。 So this code snipped contains not a memory leak but the opposite, a double free (in the worst case).所以这个代码片断的,包含不是内存泄漏,但相反,双重释放(在最坏的情况下)。Note that Instruments sometimes generates false positives and/or reports memory leaks in the Foundation itself (yep, they make mistakes too :-).需要注意的是仪器有时会产生假阳性和/或报告内存泄漏基金会本身(是的,他们会犯错误太:-)。
The fastest way to alloc/release an object is to avoid convenience initializers (like imageWithData:) and instead to something like最快的方式是为了避免分配/释放一个对象方便的初始化(如imageWithData :)喜欢的东西,而
This will allocate and release your object right away and not wait until the autorelease pool is cleaned.这将分配和释放你的对象,而不是等到autorelease池清洗。
But please note too, that a memory leak is generally not memory that is not yet freed, but that is lost and cannot be freed anymore , so an object which will be deallocated by the autorelease pool is not considered a memory leak.但也请注意,内存泄漏一般是没有尚未释放的内存,但丢失,不能再被释放 ,所以一个对象,该对象将被释放autorelease池不被认为是内存泄漏。