内存泄漏NSAutoreleasePool [英] Memory leak NSAutoreleasePool
问题描述
使用仪器时,这段代码导致内存泄漏,我不明白为什么!
With instruments i got a memory leak on this piece of code and i don't understand why!
-(void)goToThisUrl:(id) targetUrl
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (someCondition) {
// Doing some stuff here
}
// Instruments show memory leak on data
else {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: targetUrl]];
myTargetImage = [UIImage imageWithData:data];
// When releasing data(because data retainCount = 2), i got:
// Incorrect decrement of the reference count of an object that is not owned at this point by the caller
//[data release];
}
[pool release];
}
谢谢
推荐答案
以上没有泄漏.在您删除并替换为"someCondition"和在这里做一些事情"的零件中,可能会有一个或多个泄漏,但是除非您发布完整的代码,您正在使用Instruments进行真正的测试.
There is no leak in the above. There may be one or more leaks in the parts you've deleted and replaced with "someCondition" and "Doing some stuff here," but no one here can help with that unless you post the complete code that you're really testing with Instruments.
也:"//释放数据时(因为数据keepCount = 2)..."停止.正确的.那里.忽略retainCount.释放对象是因为您使用暗示所有权的方法创建了对象,或者因为保留了对象.您从不释放对象仅仅是因为对象的retainCount具有您不期望或无法理解的值.有关详细信息,请阅读Apple的《内存管理编程指南》 . .
Also: "// When releasing data(because data retainCount = 2) ..." Stop. Right. There. Ignore retainCount. You release an object because you've created it using a method that implies ownership, or because you've retained it. You NEVER release an object just because its retainCount has a value you didn't expect or don't understand. Read Apple's Memory Management Programming Guide for details.
这篇关于内存泄漏NSAutoreleasePool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!