0

With instruments i got a memory leak in this method of a detached thread :

-(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];
}

I don't understand this leak.

Thanks.

4

1 回答 1

1

下意识的可能性,因为该代码中没有内存所有权问题:

  • 如果某人在其他地方保留但未正确释放 myTargetImage,则数据可能会泄漏,Instruments 会向您显示指示的位置,因为它报告对象的创建位置,而不是泄漏的位置
  • “在大多数情况下,UIKit 类应该只在应用程序的主线程中使用。” (来源);除非你有特定的授权 UIImage +imageWithData 是安全的(我找不到任何东西,但苹果已经很难找到这种细节)那么你所做的在技术上是非法的,并且像泄漏这样的功能奇怪应该不要惊讶。
于 2011-04-23T17:55:25.547 回答