1

I have faced problem when I using AddressBook framework (Core Foundation) which make memory crash for a NSMutableDictionary variable using to set phone number and another fields in address book such as email below. This case happen as a first time when I call this method passing the data into ABNewPerson after that when cancel or save contact and come back to my view controller and click another time to call this method give me error on phone number data in NSMutabledictionary may code is below:

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

And here the crash :

 [CFString release]: message sent to deallocated instance 
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
wod
  • 812
  • 1
  • 10
  • 23
  • did u put the breakpoint and find where the error occured? – Sudha Tiwari Feb 11 '13 at 08:47
  • Yes , i do it the problem in this dictionary (dictionaryDetail) which is release from ARC may be after i open address book view controller and dismissed it – wod Feb 11 '13 at 08:50
  • it give me data on first view but after that crashed – wod Feb 11 '13 at 08:50
  • I want to notice that this dictionary is passed to this view controller by didSelectRow from table view – wod Feb 11 '13 at 09:04

1 Answers1

2

I have resolved , the problem in the phone ABMutableMultiValueRef correct answer is below :

Previous answer :

   ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABPersonPhoneProperty);

Correct Answer :

  ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABMultiStringPropertyType);
wod
  • 812
  • 1
  • 10
  • 23