我有以下代码:
char *get_string_property_2(IOHIDDeviceRef device, CFStringRef prop)
{
CFStringRef str = IOHIDDeviceGetProperty(device, prop);
if(str)
{
CFIndex str_len = CFStringGetLength(str);
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
有时(并非总是)我CFStringGetLength使用以下日志崩溃:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00007fff770083a8
Thread 8 Crashed:
0 libobjc.A.dylib 0x00007fff88e22097 objc_msgSend + 23
1 libtobii_usb.dylib 0x00000001081c4f72 get_string_property_2 + 130 (macosx.c:99)
2 serviced 0x00000001081a5fad device_removed_callback + 285 (daemond_mac.c:614)
3 libsystem_pthread.dylib 0x00007fff8ae35899 _pthread_body + 138
4 libsystem_pthread.dylib 0x00007fff8ae3572a _pthread_start + 137
5 libsystem_pthread.dylib 0x00007fff8ae39fc9 thread_start + 13
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?在调用之前我正在检查str是否为NULL,CFStringGetLength显然它还不够.
编辑:
我仍然在这个崩溃,总是在同一个地方.
如果它是一个坏指针,为什么不会崩溃上面的一行IOHIDDeviceGetProperty?
有没有其他方法可以得到一个长度CFStringRef?
我怀疑api中有一个bug.
Some thoughts. First, IOKit's lifecycles have always been tricky for me. There are many areas where you have to be super careful about retaining objects, which are "consumed" by API calls.
Second, your crash is happening in device_removed_callback, which further makes me suspect that some object is getting deallocated behind the scenes, but you aren't noticing until the callback occurs.
坏指针并不总是为空,并且经常指向映射到进程中的内存,但并不指向您认为应该指向的内容。这通常称为悬空指针,并且非常非常常见。通常也很难调试。
我敢打赌,这是由于 IOKit 代码的另一个区域中的对象生命周期问题造成的,并在此处体现出来。尝试使用 NSZombies(它也可能适用于 CF 对象,我不记得了)。另外,请仔细检查注册设备通知的区域是否在内存管理方面做得正确。我在该领域遇到了很多错误。
| 归档时间: |
|
| 查看次数: |
306 次 |
| 最近记录: |