{"issue":{"created_at":"2011-10-28T00:09:33Z","state":"open","updated_at":"2011-11-21T16:38:25Z","comments":11,"user":{"gravatar_id":"17e41bb5ef769454388371c036806d96","id":780632,"url":"https://api.github.com/users/mrpippy","login":"mrpippy","avatar_url":"https://secure.gravatar.com/avatar/17e41bb5ef769454388371c036806d96?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"Hi Alan,\r\n\r\nMy 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.\r\n\r\nIt 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.\r\n\r\nThe obvious fix is that the return value of `hid_init()` should always be checked before `hid_mgr` is used.\r\nBut why is `hid_init`/`IOHIDManagerOpen()` failing?\r\n\r\nThis 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?\r\n\r\n```\r\nException Type:  EXC_BAD_ACCESS (SIGSEGV)\r\nException Codes: KERN_INVALID_ADDRESS at 0x0000000000000010\r\nCrashed Thread:  0  Dispatch queue: com.apple.main-thread\r\n \r\nThread 0 Crashed:  Dispatch queue: com.apple.main-thread\r\n0   com.apple.framework.IOKit     0x00007fff85391024\r\nIOHIDManagerCopyDevices + 4\r\n1   ...electronics.FirmwareUpdater 0x0000000100017cfb hid_enumerate + 63\r\n2   ...electronics.FirmwareUpdater 0x000000010000eed9\r\nComm::startUSBPollingTimer() + 25\r\n3   ...electronics.FirmwareUpdater 0x00000001000038f8\r\nMainWindow::MainWindow(QWidget*) + 680\r\n4   ...electronics.FirmwareUpdater 0x000000010000da17 main + 274\r\n5   ...electronics.FirmwareUpdater 0x000000010000362c start + 52\r\n```","assignee":null,"number":26,"id":2074363,"labels":[],"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"title":"Mac: segfault in IOHIDManagerCopyDevices","closed_at":null,"html_url":"https://github.com/signal11/hidapi/issues/26","url":"https://api.github.com/repos/signal11/hidapi/issues/26","milestone":null},"action":"created","comment":{"created_at":"2011-11-21T16:38:25Z","updated_at":"2011-11-21T16:38:25Z","user":{"gravatar_id":"4669b356641bd29d55c871b831634c6d","id":168999,"url":"https://api.github.com/users/signal11","login":"signal11","avatar_url":"https://secure.gravatar.com/avatar/4669b356641bd29d55c871b831634c6d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"Darthrake, maybe I'm missing something here. You say changing the return logic of init_hid_manager() makes it actually work? I can see making it not crash, but actually work?\r\n\r\nIf IOHidManagerCreate() fails, how can anything else possibly work?\r\n\r\nLooking back more closely, it seems like we're talking about two distinct problems here:\r\n1. IOHidManagerCreate() returns a NULL. (serious problem, not recoverable it would seem)\r\n2. IOHidManagerOpen() returns a bitmask error code.\r\n\r\nFor number 2, we can just detect whether the code is recoverable or not.\r\n\r\nDarthrake, you also say that hid_exit() is causing you a problem. Can you elaborate on what you mean there? hid_exit() sets hid_mgr() back to NULL in all cases.\r\n\r\n","id":2817388,"url":"https://api.github.com/repos/signal11/hidapi/issues/comments/2817388"}}