views:

208

answers:

1

I've got the following class that is a wrapper around an ABPerson (ABRecordRef):

@interface Recipient : NSObject {
 ABRecordRef person;
}

- (id)initWithPerson:(ABRecordRef)person;

@end

@implementation 

- (id)initWithPerson:(ABRecordRef)_person {
 if(self = [super init]) person = CFRetain(_person);
 return self;
}

- (void)dealloc {
 if(person) CFRelease(person);

 [super dealloc];
}

@end

I've left some of the methods out, but they aren't relevant to this question.

Everything works fine, except I get an EXC_BAD_ACCESS on the if(person) CFRelease(person); line. Why does this happen? I'm not calling CFRelease or CFRetain at all anywhere else in my app.

Edit, another note: If I add this right before the CFRelease line:

NSLog(@"retain count is %d", CFGetRetainCount(person));

It prints retain count is 1

+2  A: 

I feel sheepish. I had the following code:

// getting the address
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multi, addressIndex);

NSString *_street = (NSString *)CFDictionaryGetValue(dict,
    kABPersonAddressStreetKey);
if(_street != nil) street = _street;    

Apparently, CFDictionaryGetValue doesn't make a copy... and that's probably why it doesn't have Copy in the function name. I wasn't retaining it, and so CFRelease(person) was causing street to be released after it was already reclaimed.

I guess the rest of the code in the class was relevant. Sorry, I'll try to not make this mistake when posting here in the future.

synic