0

I'm trying to open a connection to a camera from a raspberry pi 2 over usb. I'm able to detect the camera but when I try to open a connection using

libusb_open_device_with_vid_pid(null, vendor id, product id);

But I receive a segmentation fault. I've narrowed it down and that line of code is what causes the segmentation fault.

void opendevice(){
    libusb_device_handle* dev;
    struct libusb_device_descriptor* desc;
    int err;

dev = libusb_open_device_with_vid_pid(NULL,0x2a0b,0x00f8);

   if (dev == NULL){
        printf("device not found\n");
       }

else {
     err = libusb_claim_interface(dev, 0);
     }    
}

The message from the pi opened over putty on my computer is.

Segmentation fault

Any ideas as to what I am doing wrong?

Mathew Wright
  • 95
  • 1
  • 11

1 Answers1

1

You are getting NULL for dev, and using it anyway. You could add a return statement after the printf(), or an else before the libusb_claim_interface().

donjuedo
  • 2,475
  • 18
  • 28
  • Heh.. missed it surprisingly..It's like "is there a check? Yes there is." But without paying attention what we do with this check.. – Eugene Sh. Aug 19 '15 at 15:12
  • Even if that would be a problem. The problem that returns the segmentation fault is the code that I have specified above. When putting a printf statement before the if statement it is not hit. The problem is within the libusb_open_device_with_vid_pid function or my use of it. – Mathew Wright Aug 19 '15 at 15:30
  • @MathewWright, Well, I did assume the library call has no internal bug. The parameters going in are simple, and NULL is permitted as the first param. So how did you determine the open is where the crash is? – donjuedo Aug 19 '15 at 15:33
  • @MathewWright, Also, it wouldn't hurt to add the return mentioned, retest, and edit the post, just to eliminate distraction (If indeed I'm mistaken). – donjuedo Aug 19 '15 at 15:36
  • @donjuedo, I assumed it would be fine also. I used simple printf statements before and after the open function. The printf after the open function did not show. The program terminates at that function call because of a segmentation error. – Mathew Wright Aug 19 '15 at 15:38
  • @MathewWright, that is what I suspected. `printf()` buffers output, so the absence of text is misleading. That's also why I suggested adding the change anyway. If the crash is in the claim, and the return is added, the `printf()` output will get a chance to flush. – donjuedo Aug 19 '15 at 15:45
  • @donjuedo I added the change you suggested above to the code above. I have also removed the entire check statement. Sadly I still get the error. – Mathew Wright Aug 19 '15 at 15:49