问题描述
我做了Analyze
并在下面的代码中得到了potential leak of an object stored into
警告
I did Analyze
and got potential leak of an object stored into
warning in my code below
+ (ABAddressBookRef)getABAddressBookRef {
unsigned long long lastAddressBookGeneration = _addressBookGeneration;
unsigned long long addressBookGeneration =[[NSThread currentThread].threadDictionary[kAddressBookGeneration] unsignedLongLongValue];
ABAddressBookRef addressBook = (__bridge ABAddressBookRef)[NSThread currentThread].threadDictionary[kAddressBook];
if (addressBook == nil || (addressBookGeneration != lastAddressBookGeneration)) {
if (addressBook) {
if ([NSThread isMainThread]) {
ABAddressBookUnregisterExternalChangeCallback(addressBook,_ExternalChangeCallback, nil);
}
[[NSThread currentThread].threadDictionary removeObjectForKey:kAddressBook];
}
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook != nil) {
if ([NSThread isMainThread]) {
ABAddressBookRegisterExternalChangeCallback(addressBook,_ExternalChangeCallback, nil);
}
[NSThread currentThread].threadDictionary[kAddressBook] = (__bridge id) addressBook;
[NSThread currentThread].threadDictionary[kAddressBookGeneration] = [NSNumber numberWithUnsignedLongLong:lastAddressBookGeneration];
}
}
return addressBook;
}
我环顾四周,发现问题可能是ABAddressBookCreateWithOptions(NULL, NULL);
我可能需要在调用之后调用CFRelease().但是,接下来,我的下一个问题浮出水面:在这种方法中,我需要返回addressBook.我该如何实现1)返回ABAddressBookRef 2)正确调用CFRelease()这样我就不会收到内存泄漏警告了?
I looked around SO and found the problem could be ABAddressBookCreateWithOptions(NULL, NULL);
I probably need to call CFRelease() after calling that. But then, my next question came up: in this method, I need to return addressBook. How can I achieve both 1) return the ABAddressBookRef 2) properly call CFRelease() so I don't get the memory leak warning?
这有意义吗?
+ (ABAddressBookRef)getABAddressBookRef {
unsigned long long lastAddressBookGeneration = _addressBookGeneration;
unsigned long long addressBookGeneration =[[NSThread currentThread].threadDictionary[kAddressBookGeneration] unsignedLongLongValue];
ABAddressBookRef addressBook = (__bridge ABAddressBookRef)[NSThread currentThread].threadDictionary[kAddressBook];
if (addressBook == nil || (addressBookGeneration != lastAddressBookGeneration)) {
if (addressBook) {
if ([NSThread isMainThread]) {
ABAddressBookUnregisterExternalChangeCallback(addressBook,_ExternalChangeCallback, nil);
}
[[NSThread currentThread].threadDictionary removeObjectForKey:kAddressBook];
}
ABAddressBookRef newAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);
addressBook = newAddressBook;
if (newAddressBook != nil) {
if ([NSThread isMainThread]) {
ABAddressBookRegisterExternalChangeCallback(newAddressBook, _ExternalChangeCallback, nil);
}
[NSThread currentThread].threadDictionary[kAddressBook] = (__bridge id) newAddressBook;
[NSThread currentThread].threadDictionary[kAddressBookGeneration] = [NSNumber numberWithUnsignedLongLong:lastAddressBookGeneration];
CFRelease(newAddressBook);
}
}
return addressBook;
}
推荐答案
您可以轻松模拟自动发布
you can simulate autorelease easily
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
CFRelease(newAddressBook);
});
这篇关于当还需要返回对象时释放对象的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!