使用仪器,我在这段代码上出现了内存泄漏,我不明白为什么!
-(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];
}
Thanks
上面没有泄漏。那里may是您已删除并替换为“someCondition”和“在这里做一些事情”的部分中的一个或多个泄漏,但这里没有人可以提供帮助,除非您发布complete您真正使用 Instruments 测试的代码。
另外:“//释放数据时(因为数据retainCount = 2)...”停止。正确的。那里。忽略保留计数。您释放一个对象是因为您使用暗示所有权的方法创建了它,或者因为您保留了它。你NEVER释放一个对象只是因为它的retainCount有一个你没有预料到或不理解的值。阅读苹果的内存管理编程指南 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)