问题描述
使用Instruments,我继续指向UIImage的内存泄漏。
我想我正在分配和释放内存。仪器中泄漏的对象被描述为NSConcreteData
Using Instruments, I keep on getting pointed to a memory leak with a UIImage.
I think I'm assigning and releasing the memory correctly. The leaked object in instruments is described as NSConcreteData
以下是分配和释放UIImage的正确方法吗?
Is the following the correct way to assign and release a UIImage?
UIImage* flagimg = [UIImage imageWithData: [NSData dataWithContentsOfURL:url2]];
[flagimg release];
flagimg =nil;
推荐答案
[UIImage imageWithData:]
返回一个自动释放的对象,不应该再次释放。因此,此代码剪切不包含内存泄漏,但相反,双重免费(在最坏的情况下)。
[UIImage imageWithData:]
returns an autoreleased object, which should not be released by you again. 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 :-).
分配/释放对象的最快方法是避免方便初始化器(如imageWithData :)而是像
The fastest way to alloc/release an object is to avoid convenience initializers (like imageWithData:) and instead to something like
这将立即分配和释放您的对象,而不是等到清理自动释放池。
This will allocate and release your object right away and not wait until the autorelease pool is cleaned.
但是请注意,内存泄漏通常不是尚未释放的内存,但是它会丢失并且不能再被释放,所以一个对象这将由a解除分配utorelease pool不被视为内存泄漏。
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.
这篇关于什么是将UIImage分配给内存并将其释放到iphone的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!