1

当我使用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 
4

1 回答 1

2

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

上一个答案:

   ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABPersonPhoneProperty);

正确答案 :

  ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABMultiStringPropertyType);
于 2013-02-11T12:02:52.067 回答