iPhone ABPeoplePickerNavigationController 内存泄漏
Posted
技术标签:
【中文标题】iPhone ABPeoplePickerNavigationController 内存泄漏【英文标题】:iPhone ABPeoplePickerNavigationController Memory Leaks 【发布时间】:2011-08-23 13:03:00 【问题描述】:我正在检查我的应用程序以消除所有内存泄漏,并且分析工具使用 abpeoplepickernavigationcontroller 发现了泄漏。
我知道这与复制方法有关?但不知道如何在编写时间和地点发布它。
我基本上需要呈现模态视图,选择电话号码,然后将其拖回文本字段。这是我的代码
-(IBAction)openAddressBook
ABPeoplePickerNavigationController *peoplepicker = [[ABPeoplePickerNavigationController alloc] init];
peoplepicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplepicker animated:YES];
[peoplepicker release];
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
[self dismissModalViewControllerAnimated:YES];
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
return YES;
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
//Retrieving the phone number property of ABRecordRef
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *brokenNumber = [phone componentsSeparatedByString:@" "];
phone = [brokenNumber componentsJoinedByString:@""];
if(![phonenumber.text isEqualToString:@""])
phonenumber.text = [NSString stringWithFormat:@"%@%@", phonenumber.text, @";"];
phonenumber.text = [NSString stringWithFormat:@"%@%@", phonenumber.text, phone];
[self dismissModalViewControllerAnimated:YES];
return NO;
谢谢
【问题讨论】:
【参考方案1】:问题很可能出在这里:
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
您使用ABRecordCopyValue
和ABMultiValueCopyValueAtIndex
获得对象副本,但它们不会被释放。
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
if (phoneProperty)
CFRelease(phoneProperty);
NSString *trimmedPhone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (phone)
CFRelease(phone);
【讨论】:
完全正确,需要 CFRelease。令人讨厌的是,它看起来像 ABPeoplePickerNavigationController 本身一样泄漏。以上是关于iPhone ABPeoplePickerNavigationController 内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章
在 iphone 5 和 iphone 6 中动态设置字体大小