Results 1 to 8 of 8

Thread: Program crashes after sending data over libusb/Qthread for 3d time

  1. #1
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Program crashes after sending data over libusb/Qthread for 3d time

    yet another problem! i dont think this has naything to do with libusb but more with the Thread im running.

    in my main thread i call a function that sets a character array in the 2nd thread (usbcom) and set a boolean to indicate there is data to send.

    Qt Code:
    1. void Project::sendSettings()
    2. {
    3. unsigned char *b;
    4. int r, transf;
    5. if (ui->checkBox_RPM->isChecked())
    6. {
    7. sprintf((char*)b,"*1 %d %d %d",ui->spinBox_RPM->value(), ui->spinBox_KOPPEL->value(), ui->spinBox_over->value());
    8. a.append((const char*)b);
    9. usbcom->data = b;
    10. usbcom->send = true;
    11. }
    12. else if (ui->checkBox_TEMP->isChecked())
    13. {
    14. sprintf((char*)b,"*0 %d %d", (char)ui->spinBox_TEMP->value(), (char)ui->spinBox_over->value());
    15. a.append((const char*)b);
    16. usbcom->data = b;
    17. usbcom->send = true;
    18. }
    19. else
    20. {
    21. box.setText("Must at least check one option!");
    22. box.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    the above function is called by this SLOT:

    Qt Code:
    1. void Project::button_send()
    2. {
    3. if(!usbcom->isRunning())
    4. {
    5. if(checkConnection())
    6. {
    7. sendSettings();
    8. }
    9. }
    10. else
    11. {
    12. sendSettings();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    placing a breakpoint on the first line of this slot shows me that when checking "if(!usbcom->isRunning)" for the thirth time stops the program with a segmentation fault.
    gdb output:

    Qt Code:
    1. Program received signal SIGSEGV, Segmentation fault.
    2. [Switching to Thread 0xb6d64700 (LWP 20232)]
    3. 0xb71bddd9 in QMutex::lock (this=0x9309cdc)
    4. at /var/tmp/qt-x11-src-4.5.3/src/corelib/thread/qmutex.cpp:152
    5. 152 /var/tmp/qt-x11-src-4.5.3/src/corelib/thread/qmutex.cpp: No such file or directory.
    6. in /var/tmp/qt-x11-src-4.5.3/src/corelib/thread/qmutex.cpp
    To copy to clipboard, switch view to plain text mode 

    what could cause this?

    help is much appreciated

    Sisco

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    So where is the thread in all this?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    Quote Originally Posted by wysota View Post
    So where is the thread in all this?
    Qt Code:
    1. void UsbCom::run()
    2. {
    3. int r;
    4. r = libusb_init(NULL);
    5. send = false;
    6. if (r < 0)
    7. {
    8. emit error("Failed to initialize USB!");
    9.  
    10. }
    11. devh = libusb_open_device_with_vid_pid(NULL, idVENDOR, idPRODUCT);
    12. if(!devh)
    13. {
    14. emit error("Could not locate device");
    15. }
    16. else
    17. {
    18. r = libusb_kernel_driver_active (devh, 0);
    19. if (r != 0)
    20. {
    21. if(r==1)
    22. {
    23. r = libusb_detach_kernel_driver(devh, 0);
    24. if (r > 0)
    25. {
    26. emit error("some other random error:"+ QString::number(r));
    27. }
    28. }
    29. }
    30. r = libusb_claim_interface(devh, 0);
    31. if (r < 0)
    32. {
    33. emit error("usb_claim_interface error"+ QString::number(r));
    34. }
    35. int a = 0;
    36. unsigned char irqbuf[64];
    37. irq_transfer = libusb_alloc_transfer(0);
    38. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR_IN, irqbuf,sizeof(irqbuf), ep_irq_in_cb, &a, 0);
    39. r = libusb_submit_transfer(irq_transfer);
    40. while(1)
    41. {
    42. if(send)
    43. {
    44. sendData();
    45. }
    46. if (a == 1)
    47. {
    48. emit dataRecieved(irqbuf);
    49. a = 0;
    50. }
    51. r = libusb_handle_events(NULL);
    52. }
    53. }
    54. libusb_close(devh);
    55. }
    56.  
    57. void UsbCom::sendData()
    58. {
    59. int r,transf;
    60. a.append((const char*)data);
    61. r = libusb_interrupt_transfer(devh, EP_INTR_OUT, data, a.length(),&transf,1000);
    62. if (r == 0)
    63. {
    64. emit error("Configuration sent");
    65. }
    66. send = false;
    67. }
    68.  
    69. void ep_irq_in_cb(libusb_transfer *transfer)
    70. {
    71. if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
    72. {
    73. fprintf(stderr, "uncompleted transter\n");
    74. }
    75. else
    76. {
    77. int *data = static_cast<int*>(transfer->user_data);
    78. *data = 1;
    79. }
    80.  
    81. if (libusb_submit_transfer(transfer) < 0)
    82. {
    83. fprintf(stderr,"could not resubmit irq \n");
    84. //exit(1);
    85. }
    86. }
    To copy to clipboard, switch view to plain text mode 

    sorry about that

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    How about doing proper synchronization of shared variables?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    im sorry, i am still very new to this.
    i have read about QMutex and QWaitCondition but i cant seem to figure out how to implement this in my code.

    i tried different aproaches:

    Qt Code:
    1. void UsbCom::run()
    2. {
    3. int r;
    4. r = libusb_init(NULL);
    5. send = false;
    6. if (r < 0)
    7. {
    8. emit error("Failed to initialize USB!");
    9. }
    10. devh = libusb_open_device_with_vid_pid(NULL, idVENDOR, idPRODUCT);
    11. if(!devh)
    12. {
    13. emit error("Could not locate device");
    14. }
    15. else
    16. {
    17. r = libusb_kernel_driver_active (devh, 0);
    18. if (r != 0)
    19. {
    20. if(r==1)
    21. {
    22. r = libusb_detach_kernel_driver(devh, 0);
    23. if (r > 0)
    24. {
    25. emit error("some other random error:"+ QString::number(r));
    26. }
    27. }
    28. }
    29. r = libusb_claim_interface(devh, 0);
    30. if (r < 0)
    31. {
    32. emit error("usb_claim_interface error"+ QString::number(r));
    33. }
    34. int a = 0;
    35. unsigned char irqbuf[64];
    36. irq_transfer = libusb_alloc_transfer(0);
    37. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR_IN, irqbuf,sizeof(irqbuf), ep_irq_in_cb, &a, 0);
    38. r = libusb_submit_transfer(irq_transfer);
    39. while(1)
    40. {
    41. wait.wait(mutex); //QMutex *mutex
    42. if(send)
    43. {
    44. sendData();
    45. }
    46. if (a == 1)
    47. {
    48. emit dataRecieved(irqbuf);
    49. a = 0;
    50. }
    51. r = libusb_handle_events(NULL);
    52. }
    53. }
    54. libusb_close(devh);
    55. }
    56.  
    57. void UsbCom::sendData()
    58. {
    59. int r,transf;
    60. a.append((const char*)data);
    61. r = libusb_interrupt_transfer(devh, EP_INTR_OUT, data, a.length(),&transf,1000);
    62. if (r == 0)
    63. {
    64. emit error("Configuration sent");
    65. }
    66. send = false;
    67. }
    68.  
    69. void ep_irq_in_cb(libusb_transfer *transfer)
    70. {
    71. if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
    72. {
    73. fprintf(stderr, "uncompleted transter\n");
    74. }
    75. else
    76. {
    77. int *data = static_cast<int*>(transfer->user_data);
    78. *data = 1;
    79. }
    80.  
    81. if (libusb_submit_transfer(transfer) < 0)
    82. {
    83. fprintf(stderr,"could not resubmit irq \n");
    84. //exit(1);
    85. }
    86. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. usbcom->mutex = &mutex
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Project::sendSettings()
    2. {
    3. unsigned char *b;
    4. int r, transf;
    5. if (ui->checkBox_RPM->isChecked())
    6. {
    7. sprintf((char*)b,"*1 %d %d %d",ui->spinBox_RPM->value(), ui->spinBox_KOPPEL->value(), ui->spinBox_over->value());
    8. a.append((const char*)b);
    9. usbcom->data = b;
    10. usbcom->send = true;
    11. }
    12. else if (ui->checkBox_TEMP->isChecked())
    13. {
    14. sprintf((char*)b,"*0 %d %d", (char)ui->spinBox_TEMP->value(), (char)ui->spinBox_over->value());
    15. a.append((const char*)b);
    16. usbcom->data = b;
    17. usbcom->send = true;
    18. }
    19. else
    20. {
    21. box.setText("Must at least check one option!");
    22. box.exec();
    23. }
    24. usbcom->wait.wakeAll();
    To copy to clipboard, switch view to plain text mode 

    this compiles fine but still gives a seg fault.
    could you please explain me a little on how i should implement this?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    Learn to use QMutex. There is a nice example of protecting a shared variable in its docs.

    By the way, there is no reason for you to use threads here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    how would i get this so i dont need to use a thread.
    right now the thread has a "while(1)" loop to check for a completed transfer.
    i cant see a way to have this running without hanging the main thread.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    Quote Originally Posted by sisco View Post
    how would i get this so i dont need to use a thread.
    right now the thread has a "while(1)" loop to check for a completed transfer.
    i cant see a way to have this running without hanging the main thread.
    Use signals and slots (probably with a timer) instead of the while loop. There is no point in using up all your cpu resources without a reason.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. real time data display
    By hammer256 in forum Qt Programming
    Replies: 13
    Last Post: 25th March 2013, 17:47
  2. Program crashes on creating new dialog
    By eekhoorn12 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 12:52
  3. Replies: 3
    Last Post: 19th February 2009, 20:01
  4. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 05:19
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 10:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.