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 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 :)喜欢的东西,而
NSData* data = [[NSData alloc] initWithContentsOfURL:url]]; UIImage* img = [[UIImage alloc] initWithData:data]; [data release]; // use your image [img release];
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池不被认为是内存泄漏。
both imageWithData and dataWithContentsOfURL return autoreleased objects, so you should have no memory leaks in that code snippet.imageWithData和autoreleased对象dataWithContentsOfURL回报,所以你应该有没有内存泄漏的代码片段。
Since flagimg is returned autoreleased, your [flagimg release]; call is not needed; you're over-releasing that object. ,由于flagimg返回自动释放,你的[flagimg release];不需要调用的,你是过度释放该对象。
as a general rule you can say作为一般规则,你可以说
if you create an object an theres a "init","copy" or "retain" in it, you have to release it.如果你创建了一个对象,一个世界上的“初始化”,“复制”或“保留”中,你必须释放它。if not, you get an autoreleased object.如果没有,你得到一个自动释放的对象。
thats not always true, but in most cases那并不总是正确的,但在大多数情况下
[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池不被认为是内存泄漏。