ARC 中 CFArray 的内存泄漏
我使用 ARC 制作了一个 iPhone 应用程序,可以访问地址簿中的每个条目,然后访问每个人的每个地址。数据存储在 CFArray 中,CFArray 可以免费桥接到 NSArray。代码如下。
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSArray *peopleArray =[(__bridge NSArray *) arrayRef copy];
CFRelease(arrayRef);
arrayRef = nil;
for(id personId in peopleArray)
{
ABRecordRef person = (__bridge ABRecordRef) personId;
//process other attributes of the address book
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
CFArrayRef addressRef = ABMultiValueCopyArrayOfAllValues(multi);
NSArray *addressArray = [(__bridge NSArray *) addressRef copy];
for(NSDictionary *address in addressArray)
{
//process the addresses
}
CFRelease(addressRef);
addressRef = nil;
}
根据我在互联网上和苹果内存管理指南中的研究,这看起来是正确的方法。问题是当我运行代码时,它停止在“CFRelease(addressRef)”上,并用文本“Thread 1”突出显示为绿色(不确定此错误意味着什么)。我也尝试将 CFRelease 放在 for 循环之前,但出现了同样的问题。
如果我删除 CFRelease,它会编译,但在创建 addressArray 时会出现内存泄漏。有谁知道如何解决这个问题?我似乎无法用 ARC 来解决这个问题。
I've made an iPhone app using ARC that accesses every entry in the address book, and then every address for every person. The data is stored in CFArrays, which are toll-free bridged to NSArrays. The code is below.
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSArray *peopleArray =[(__bridge NSArray *) arrayRef copy];
CFRelease(arrayRef);
arrayRef = nil;
for(id personId in peopleArray)
{
ABRecordRef person = (__bridge ABRecordRef) personId;
//process other attributes of the address book
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
CFArrayRef addressRef = ABMultiValueCopyArrayOfAllValues(multi);
NSArray *addressArray = [(__bridge NSArray *) addressRef copy];
for(NSDictionary *address in addressArray)
{
//process the addresses
}
CFRelease(addressRef);
addressRef = nil;
}
From what I've researched on the internet and in Apple's Memory Management guides, this looks like the proper way to do it. The problem is when I got to run the code, it halts on "CFRelease(addressRef)", highlighted green with text "Thread 1" (not sure what this error means). I've also tried putting the CFRelease before the for loop, but the same issue occurs.
If I remove the CFRelease, it compiles, but there is a memory leak at the creation of addressArray. Does anyone know how to solve this problem? I can't seem to figure it out using ARC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是 NSArray *peopleArray =[(__bridge NSArray *) arrayRef copy]; CFRelease(arrayRef);,使用
NSArray *peopleArray = CFBridgingRelease(arrayRef)
。这会将对象的所有权转移给 ARC。Instead of
NSArray *peopleArray =[(__bridge NSArray *) arrayRef copy]; CFRelease(arrayRef);
, useNSArray *peopleArray = CFBridgingRelease(arrayRef)
. This transfers ownership of the object to ARC.每当您在方法名称中看到“Copy”时,您应该使用
(__bridge_transfer*)
然后 ARC 将负责释放该对象。
所以你的代码将如下所示:
Whenever you see "Copy" in a method name you should use
(__bridge_transfer <ObjectType> *)
Then ARC will be responsible for releasing the object.
So your code will look like: