我正在通过我的应用程序查找所有内存泄漏,而分析工具使用abpeoplepickernavigationcontroller发现泄漏。
我知道这与复制方法有关吗?但是不知道如何在写作时间和写作时间释放它。
我基本上需要提供模态视图,选择phonenumber然后将其拖回文本字段。继承我的代码
-(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;
}
由于
答案 0 :(得分:2)
问题很可能就在这里:
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);
}