ABAdressbook 内存泄漏
我在以下方法中遇到内存泄漏:
- (void) SyncContactData
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i = 0 ; i < nPeople ; i++ )
{
//dicContact = [[NSMutableDictionary alloc] init];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i );
NSString *str = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *strSub = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
int ii = [[NSString stringWithFormat:@"%@",strSub] length];
if(str != nil || ii == 0)
[arrNames addObject:strSub];
else
[arrNames addObject:@""];
CFTypeRef multival = ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSArray *arrayPh = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival);
if([arrayPh count] > 0)
[arrPhone addObject:[arrayPh objectAtIndex:0]];
else
[arrPhone addObject:@""];
CFRelease(multival);
}
CFRelease(addressBook);
CFRelease(allPeople);
}
此处发生泄漏:
NSString *str = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *strSub = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSArray *arrayPh = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival);
I am getting memory leaks in the following method :
- (void) SyncContactData
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i = 0 ; i < nPeople ; i++ )
{
//dicContact = [[NSMutableDictionary alloc] init];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i );
NSString *str = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *strSub = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
int ii = [[NSString stringWithFormat:@"%@",strSub] length];
if(str != nil || ii == 0)
[arrNames addObject:strSub];
else
[arrNames addObject:@""];
CFTypeRef multival = ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSArray *arrayPh = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival);
if([arrayPh count] > 0)
[arrPhone addObject:[arrayPh objectAtIndex:0]];
else
[arrPhone addObject:@""];
CFRelease(multival);
}
CFRelease(addressBook);
CFRelease(allPeople);
}
Getting leaks here:
NSString *str = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *strSub = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSArray *arrayPh = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用完最初复制的对象时,您需要释放它们:
如果启用了 ARC,您可能需要使用
CFRelease
(并适当地转换)。You need to release objects that are originally copied when you are done with them:
If ARC is enabled, you might need to use
CFRelease
instead (and cast appropriately).那么,为什么不释放他们呢?
So, why don't released them?