signal11/hidapi — Issue #26: Mac: segfault in IOHIDManagerCopyDevices
Author: mrpippy
Created: 2011-10-28 00:09:33+00:00
State: open

Hi Alan,

My app is now released, and I've gotten two crash reports from users that show the app segfaulting in `IOHIDManagerCopyDevices()` (called from `hid_enumerate()`). The reports are from different generation MacBook Pros, both x86_64, one running 10.6.8 and the other 10.7.2. I'm using a slightly older HIDAPI version (before the changes to use threads), but I don't think `hid_init()` or `hid_enumerate()` had any changes there.

It appear that when `hid_enumerate()` calls `hid_init()`, the initialization fails somewhere, `hid_mgr` is left as `NULL` and then is passed into `IOHIDManagerCopyDevices()` causing the segfault.

The obvious fix is that the return value of `hid_init()` should always be checked before `hid_mgr` is used.
But why is `hid_init`/`IOHIDManagerOpen()` failing?

This seems like a very rare crash, it hasn't happened in any of my testing, and I haven't been able to reproduce it yet. The only other mention I found online was a [crash report](http://code.google.com/p/lightpack/issues/detail?id=139) from another Qt/HIDAPI app, which is also calling `hid_enumerate()` in a main window/app constructor. Maybe `IOHIDManagerOpen()` is failing because the run loop hasn't run yet?

```
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000010
Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 com.apple.framework.IOKit 0x00007fff85391024
IOHIDManagerCopyDevices + 4
1 ...electronics.FirmwareUpdater 0x0000000100017cfb hid_enumerate + 63
2 ...electronics.FirmwareUpdater 0x000000010000eed9
Comm::startUSBPollingTimer() + 25
3 ...electronics.FirmwareUpdater 0x00000001000038f8
MainWindow::MainWindow(QWidget*) + 680
4 ...electronics.FirmwareUpdater 0x000000010000da17 main + 274
5 ...electronics.FirmwareUpdater 0x000000010000362c start + 52
```