【问题标题】:EXC_BAD_ACCESS when trying to release an ABRecordRef尝试释放 ABRecordRef 时的 EXC_BAD_ACCESS
【发布时间】:2010-05-21 21:11:29
【问题描述】:

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

我遗漏了一些方法,但它们与这个问题无关。

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

编辑,另一个注释:如果我在 CFRelease 行之前添加这个:

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

它打印retain count is 1

【问题讨论】:

  • ABRecordRef person 对象是否有可能在调用 CFRelease 之前有一些内部对象被取消引用??
  • 有可能,但我看过了,我想不出它会在哪里。
  • 嗯,我可以看到该代码没有任何问题,所以要么你正在用其他方法做某事,要么贾斯汀有答案

标签: iphone cocoa core-foundation


【解决方案1】:

我觉得很害羞。我有以下代码:

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

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

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

我猜类中的其余代码是相关的。抱歉,以后发帖时我会尽量不犯这个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2012-12-18
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多