2009-10-23 30 views
0

下面的代码创建了一些泄漏,我不明白,因为我释放了使用Create或Copy创建的所有对象。我没有广泛使用CF对象,所以我可能会误解保留周期。任何帮助,将不胜感激请:使用CFDictionaryRef和ABMutableMultiValueRef无法解释的泄漏

ABMutableMultiValueRef webServices = ABRecordCopyValue(aRecord, kABPersonInstantMessageProperty); 

    for (CFIndex i = 0; i < ABMultiValueGetCount(webServices); i++) { 

     CFDictionaryRef webService = CFDictionaryCreateCopy(NULL, ABMultiValueCopyValueAtIndex(webServices, i)); 

     webServiceLabel = ABMultiValueCopyLabelAtIndex(webServices, i); 

     webServiceProvider = CFDictionaryGetValue(webService, kABPersonInstantMessageServiceKey); 
     webServiceUserName = CFDictionaryGetValue(webService, kABPersonInstantMessageUsernameKey); 

     // Data to be saved at this point 
     if (webService) CFRelease(webService); 
     if (webServiceLabel) CFRelease(webServiceLabel); 
    } 

    if (webServices) CFRelease(webServices); 

回答

3

这是你的问题:

CFDictionaryRef webService = CFDictionaryCreateCopy(NULL, ABMultiValueCopyValueAtIndex(webServices, i)); 

ABMultiValueCopyValueAtIndex创建这是从来没有分配给一个变量的对象。它需要被释放,但从来没有。这是内存泄漏。

+0

谢谢,问题解决了,汲取了教训! – RunLoop 2009-10-23 17:10:05

0

我像这样运行

MallocStackLogging=1 /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app/Contents/MacOS/iPhone\ Simulator 

模拟器,然后运行该应用程序,然后运行leaks appname

我把你的代码放在一个测试应用程序中,并且不能真正看到很多事情发生,基础或可可代码中深度分配形式的一些“泄漏”。

你可以用上面的方式运行你的应用程序,并显示泄漏,可以追溯到运行上面的代码的方法?

+0

嗨,我跑漏了使用仪器,这表明泄漏不是从方法调用(直接)。我只能假设必须有一种特殊的方式来发布地址簿多值词典。 – RunLoop 2009-10-23 13:24:31

+0

ABMutableMultiValueRef是一个CFTypeRef,所以它不应该是必需的。我只是在想,你所看到的泄漏并不是你应该担心的。 – duncanwilcox 2009-10-23 13:57:49