尝试释放ABRecordRef时EXC_BAD_ACCESS

时间:2010-05-21 21:11:30

标签: iphone cocoa core-foundation

我有以下类是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

我已经离开了一些方法,但它们与这个问题无关。

一切正常,但我在EXC_BAD_ACCESS行上获得了if(person) CFRelease(person);。为什么会这样?我在我的应用程序的任何其他地方都没有调用CFRelease或CFRetain。

编辑,另一个注意事项:如果我在CFRelease行之前添加此权限:

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

打印retain count is 1

1 个答案:

答案 0 :(得分:5)

我感到羞怯。我有以下代码:

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

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

显然,CFDictionaryGetValue没有复制......这可能就是它在函数名中没有Copy的原因。我没有保留它,因此CFRelease(人)导致街道在被收回后被释放。

我猜这个代码中的其余代码是相关的。对不起,我将来会发布这个错误。