iPhone ABPeoplePickerNavigationController 内存泄漏

发布于 2024-12-01 00:12:06 字数 1824 浏览 0 评论 0原文

我正在检查我的应用程序,消除所有内存泄漏,分析工具使用 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;

}

谢谢

I am going through my application killing off all the memory leaks and the analyze tool comes up with a leak using the abpeoplepickernavigationcontroller.

I understand it is something to do with a copy method? but don't know how to go about releasing it where and at the write time.

I basically need to present the modal view, select the phonenumber then drag it back into the textfield. heres my code

-(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;

}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2024-12-08 00:12:06

问题可能出在这里:

ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

您使用 ABRecordCopyValueABMultiValueCopyValueAtIndex 获取对象副本,但它们没有被释放。

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);
}

The problem is likely to be here:

ABMultiValueRef phoneProperty = ABRecordCopyValue(person, property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty, identifier);
phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

You get objects copies with ABRecordCopyValue and ABMultiValueCopyValueAtIndex and they aren't released.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文