AB使用ARC的ABNewPersonViewController导致内存崩溃

时间:2013-02-11 08:21:14

标签: iphone ios automatic-ref-counting abaddressbook

当我使用AddressBook框架(Core Foundation)时,我遇到了一个问题,它使NSMutableDictionary变量的内存崩溃,用于设置电话号码和地址簿中的其他字段,如下面的电子邮件。这种情况发生时我第一次调用此方法将数据传递到ABNewPerson之后取消或保存联系人并返回到我的视图控制器并单击另一次调用此方法时给出错误的电话号码数据在NSMutabledictionary中可能代码如下:

 ABNewPersonViewController *newPersonViewController =[[ABNewPersonViewController alloc]init];
 newPersonViewController.newPersonViewDelegate = self;
 UINavigationController *newNavigationController = [[UINavigationController alloc]

                                                       initWithRootViewController:newPersonViewController];
 newPersonViewController.displayedPerson =(ABRecordRef)[self buildContactDetails];
 [self presentModalViewController:newNavigationController animated:YES];


 - (ABRecordRef)buildContactDetails {
      ABRecordRef person = ABPersonCreate();
      CFErrorRef  error = NULL;

     // firstname
     ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge_retained CFTypeRef)(self.name.text), NULL);

    // lastname
    NSString *regionName = self.region.text;
    ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge_retained CFTypeRef)(regionName), NULL);


    //Phone

    ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABPersonPhoneProperty);
    //Here is the problem this release dictionaryDetail after come back another time
    NSString *phoneNumber = [[dictionaryDetail valueForKey:@"selectedRow"] valueForKey:@"phone" ];
    NSLog(@"NNNNN %@",phoneNumber);

    ABMultiValueAddValueAndLabel(phoneNumberMultiValue,(__bridge CFTypeRef)(phoneNumber) ,  kABOtherLabel, NULL);
    ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
    //CFRelease(phoneNumberMultiValue);



    // Start of Address
    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
    NSString *subRegion = self.subRegion.text;
    [addressDict setObject:@"" forKey:(NSString *)kABPersonAddressStreetKey];
    [addressDict setObject:@"" forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDict setObject:subRegion forKey:(NSString *)kABPersonAddressCityKey];

    ABMultiValueAddValueAndLabel(address, (__bridge_retained CFTypeRef)(addressDict), (__bridge CFStringRef)(AMLocalizedString(@"Hospital", @"Hospital")), NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, address, &error);
    //CFRelease(address);
    // End of Address
    if (error != NULL)
       NSLog(@"Error: %@", error);

   return person;
}

这里是崩溃:

 [CFString release]: message sent to deallocated instance 

1 个答案:

答案 0 :(得分:2)

我已经解决了,手机中的问题ABMutableMultiValueRef正确答案如下:

上一个回答:

   ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABPersonPhoneProperty);

正确答案:

  ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABMultiStringPropertyType);