Kerubu
June 6th, 2010, 01:04 AM
OK I expect this is something I am doing wrong but the following gives a segmentation fault in the displayDevice() function:
#include <stdlib.h>
#include <stdio.h>
#include <libusb-1.0/libusb.h>
void displayDevice(libusb_device *dev);
int main(int argc, char **argv)
{
struct libusb_context *context;
int result;
libusb_device **list;
libusb_device *found = NULL;
ssize_t cnt;
ssize_t i = 0;
int err = 0;
result = libusb_init(&context);
if (result != 0){
printf("libusb not initialised.\n");
} else {
/* Success */
printf("libusb successfully initialised.\n");
// discover devices
cnt = libusb_get_device_list(NULL, &list);
if (cnt < 0){
printf("Count less than 0.\n");
} else {
for (i = 0; i < cnt; i++) {
//libusb_device *device = list[i];
displayDevice(list[i]);
}
}
libusb_free_device_list(list, 1);
printf("De-activating libusb.\n");
libusb_exit(context);
}
exit(0);
}
void displayDevice(libusb_device *device){
int result;
struct libusb_device_descriptor *ptr_desc;
char usbver1[] = "Version 1\0";
char usbver2[] = "Version 2\0";
char *ptr_char;
//ptr_desc = &desc;
result = libusb_get_device_descriptor(device,ptr_desc);
if (result != 0){
printf("Could not get device descriptor.\n");
} else {
/* Success */
printf("Device descriptor retrieved.\n");
if (ptr_desc->bcdUSB == 0x200){
ptr_char = usbver2;
}else{
ptr_char = usbver1;
}
printf("%s\n",ptr_char);
}
free(ptr_desc);
return;
}
Now the line :
libusb_get_device_descriptor(device,ptr_desc)
ptr_desc is a pointer to a device descriptor memory for which is allocated by the above function. However when displayDevice returns there is a segmentation fault and it is something to do with the the device descriptor.
If for example I create LOCAL struct of the device descriptor and pass it to the function then all is ok, the struct is on the stack.
Am I supposed to allocate the memory for the function because the free() function doesn't seem to help and the docs in libusb don't say whether the calling function is responsible for memory release.
Your help appreciated.
#include <stdlib.h>
#include <stdio.h>
#include <libusb-1.0/libusb.h>
void displayDevice(libusb_device *dev);
int main(int argc, char **argv)
{
struct libusb_context *context;
int result;
libusb_device **list;
libusb_device *found = NULL;
ssize_t cnt;
ssize_t i = 0;
int err = 0;
result = libusb_init(&context);
if (result != 0){
printf("libusb not initialised.\n");
} else {
/* Success */
printf("libusb successfully initialised.\n");
// discover devices
cnt = libusb_get_device_list(NULL, &list);
if (cnt < 0){
printf("Count less than 0.\n");
} else {
for (i = 0; i < cnt; i++) {
//libusb_device *device = list[i];
displayDevice(list[i]);
}
}
libusb_free_device_list(list, 1);
printf("De-activating libusb.\n");
libusb_exit(context);
}
exit(0);
}
void displayDevice(libusb_device *device){
int result;
struct libusb_device_descriptor *ptr_desc;
char usbver1[] = "Version 1\0";
char usbver2[] = "Version 2\0";
char *ptr_char;
//ptr_desc = &desc;
result = libusb_get_device_descriptor(device,ptr_desc);
if (result != 0){
printf("Could not get device descriptor.\n");
} else {
/* Success */
printf("Device descriptor retrieved.\n");
if (ptr_desc->bcdUSB == 0x200){
ptr_char = usbver2;
}else{
ptr_char = usbver1;
}
printf("%s\n",ptr_char);
}
free(ptr_desc);
return;
}
Now the line :
libusb_get_device_descriptor(device,ptr_desc)
ptr_desc is a pointer to a device descriptor memory for which is allocated by the above function. However when displayDevice returns there is a segmentation fault and it is something to do with the the device descriptor.
If for example I create LOCAL struct of the device descriptor and pass it to the function then all is ok, the struct is on the stack.
Am I supposed to allocate the memory for the function because the free() function doesn't seem to help and the docs in libusb don't say whether the calling function is responsible for memory release.
Your help appreciated.