【发布时间】:2020-10-05 14:19:18
【问题描述】:
我正面临一个奇怪的 OC 异常,看起来我正在向已发布的地址发送消息,但是当我
- 尝试检查它是否为NULL,它仍然崩溃。
- 尝试调试或添加@try @catch,它什么也没捕获,但运行了几天,根本没有崩溃。
异常
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000010
VM Region Info: 0x10 is not in any region. Bytes before following region: 4304617456
REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
UNUSED SPACE AT START
--->
__TEXT 100934000-100a98000 [ 1424K] r-x/r-x SM=COW ...x/APP
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [18302]
Triggered by Thread: 22
下面的代码并不严格,只是显示逻辑(代码在一个串行调度队列中运行)
struct st_type {
void *arg;
};
static NSMutableDictionary *dictionary;
// init values
void init ()
{
// there is a static dictionary to retain the object
dictionary = [[NSMutableDictionary alloc]init];
// an object reference saved in dictionary and a struct holds it's pointer
NSObject *obj = [[NSObject alloc]init];
struct st_type *st = malloc(sizeof(struct st_type));
st->arg = (__bridge void *)obj;
dictionary[@"cached"] = obj;
// then the struct * passes to every where, I think it's safe because the object always in the dictionary.
...
}
// the only place to release the nsobject, so I think there is no chance to EXC_BAD_ACCESS, but ...
void release_object(struct st_type *st, NSObject obj){
[dictionary removeObjectForKey:@"cached"];
st->arg = NULL;
}
// some where to use the struct
void use_struct(struct st_type *st){
if(st->arg == NULL){
return;
}
// if I add try catch, it never crashs
// @try {
NSObject *obj = (__bridge NSObject*)st->arg;
[obj description]; // crash here.
// } @catch (NSException *exception) { print some log but it never reaches here... }
}
谁能帮我解决这个错误接下来我能做些什么?
【问题讨论】:
-
这是 ObjC 中的非典型模式。你想达到什么目的?
st_type的目的是在dictionary处理内存管理时传递一个无类型的对象吗?可能还有其他方法可以做到这一点。 -
它是示例,在项目中不是真实的。这样写的代码是因为它是 oc 和 c 库之间的桥梁。 st_type 是一个由 c lib 管理的结构体。
-
obj的生命周期是否与结构的生命周期相关联?dictionary的目的是什么? -
字典是oc对象的存储,防止释放,struct的生命周期比oc对象长,但是当oc需要dealloc时,会将struct的指针设置为NULL。
标签: objective-c exc-bad-access