什么是将UIImage分配给内存并将其释放到iphone的正确方法 [英] Whats the correct way to alloc a UIImage to memory and release it iphone

查看:167
本文介绍了什么是将UIImage分配给内存并将其释放到iphone的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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

NSData * data = [[NSData alloc] initWithContentsOfURL:url]];

UIImage * img = [[UIImage alloc] initWithData:data];

[数据发布];

//使用你的图片

[img release];

这将立即分配和释放您的对象,而不是等到清理自动释放池。

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的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆