iosexc-bad-accessaddressbookabpersonviewcontrolleraddressbookui

EXC_BAD_ACCESS when using an ABPersonViewControllerDelegate


I have an iOS app that shows a list of contact names in a UITableView, and displays a contact with ABPersonViewController when a cell is tapped. This project uses ARC. If I associate an ABPersonViewControllerDelegate object, the app hits an EXC_BAD_ACCESS in the AddressBookUI framework immediately after showing the person view:

(lldb) bt
* thread #1: tid = 0x2403, 0x39ee15b0 libobjc.A.dylib`objc_msgSend + 16, stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
    frame #0: 0x39ee15b0 libobjc.A.dylib`objc_msgSend + 16
    frame #1: 0x319eb880 AddressBookUI`-[ABPersonViewControllerHelper notifyScrollViewDidLoad] + 64

Here's the UITableViewDelegate method (full project linked below) that shows how the ABPersonViewController and my ABPersonViewControllerDelegate (implemented as BEVPVDelegate) are created, associated, and presented:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ABPersonViewController *pvc = [[ABPersonViewController alloc] init];
    BEVPVDelegate *pvDelegate = [[BEVPVDelegate alloc] init];

    pvc.displayedPerson = (__bridge ABRecordRef)[self.contents objectAtIndex:[indexPath row]];
    pvc.personViewDelegate = pvDelegate; // comment out this line to prevent the crash
    [self.navigationController pushViewController:pvc animated:YES];
}

Simply commenting out the line that sets the personViewDelegate property on the ABPersonViewController instance eliminates the exception, but I need this delegate object.

This is the entirety of BEVPVDelegate.m:

//
//  BEVPVDelegate.m
//  ABCrash
//

#import "BEVPVDelegate.h"

@implementation BEVPVDelegate

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSLog(@"called");
    return YES;
}

@end

Here's a bare-bones project demonstrating the problem: ABCrash.zip on Dropbox

Is there a problem in my code? Is there a workaround that allows me to use the ABPersonViewControllerDelegate?

Edit: I just ran the project in Instruments, and my personViewDelegate is getting released. If I create a retained property for the personViewDelegate in my UIViewController, the app doesn't crash.


Solution

  • How do you define the property personViewDelegate in ABPersonViewController? I think you make it weak. Just change it to strong type. The delegate object will be released after creating immediately, because weak property will not hold it. Hope it helpful.