Hi all,

I am going to ask about one strange issue.
I hope you understand that my English skills are poor.

I have a embedded(arm, linux) device and there is running a QT(4.7) Application.

QT Application consist of "many Widgets" (QPushButtons, QLabels, etc...) and "a custom thread" (pthread).
The role of the "custom thread" is to communicate with other devices.
The role of the "many Widgets" is to setText the data from the "custom thread" by pressing some button('A' Button) and display the fixed text by pressing another button('B' Button).

The issue is that the data received in the "custom thread" is not updated(call with "setText"), or the program is killed ("Segment fault").

Looking at the pseudocode will make it easier to understand.

Qt Code:
  1. QPushButton * buttonA;
  2. QPushButton * buttonB;
  3. QLabel * label;
  4.  
  5. pthread_mutex_t threadMutex;
  6. pthread_cond_t threadCond;
  7. pthread_t thread;
  8.  
  9. int recvBuffer;
  10.  
  11. static void * network_loop( void * parg )
  12. {
  13. int socketFd = -1;
  14. socketFd = socket( "Network Socket open" );
  15.  
  16. struct sockaddr_in addr;
  17. addr.sin_family = AF_INET;
  18. addr.sin_addr.s_addr = inet_addr("Server IP address");
  19. addr.sin_port = htons(Server_Listen_Port);
  20.  
  21. connect( socketFd, (struct socketaddr*)&addr, sizeof(addr) );
  22.  
  23. while( 1 ) {
  24. pthread_mutex_lock( &threadMutex );
  25. while( isThreadRunning == true ) {
  26. send( socketFd, "request data", strLength, MSG_NOSIGNAL );
  27. recv( socketFd, (void *)&recvBuffer, BUFFER_SIZE, 0 );
  28. test_widget* mTestWidget = (test_widget*)parg;
  29. mTestWidget->update_text( );
  30. }
  31. pthread_mutex_unlock( &threadMutex );
  32.  
  33. pthread_mutex_lock( &threadMutex );
  34. if( isThreadRunning == false )
  35. pthread_cond_wait( &threadCond, &threadMutex );
  36. pthread_mutex_unlock( &threadMutex );
  37. }
  38. }
  39.  
  40. void test_widget::update_text( )
  41. {
  42. label->setText( QString::number(recvBuffer) );
  43. }
  44.  
  45. test_widget::test_widget( QWidget * parent ) : QWidget(parent)
  46. {
  47. isThreadRunning = false;
  48.  
  49. pthread_mutex_init( &threadMutex, NULL );
  50. pthread_cond_init( &threadCond, NULL );
  51. pthread_create( &thread, NULL, network_loop, (void *)this );
  52.  
  53. buttonA = new QPushButton( parent );
  54. buttonB = new QPushButton( parent );
  55. label = new QLabel( parent );
  56.  
  57. buttonA->setGeometry( 0, 0, 10, 10 );
  58. buttonB->setGeometry( 20, 0, 10, 10 );
  59. label->setGeometry( 0, 40, 20, 10 );
  60. label->setText( "default" );
  61.  
  62. connect( buttonA, SIGNAL(clicked()), this, SLOT(buttonA_click()) );
  63. connect( buttonB, SIGNAL(clicked()), this, SLOT(buttonB_click()) );
  64. }
  65.  
  66. void test_widget::buttonA_click( )
  67. {
  68. isThreadRunning = true;
  69. pthread_cond_signal( &threadCond );
  70. }
  71.  
  72. void test_widget::buttonB_click( )
  73. {
  74. isThreadRunning = false;
  75. label->setText( "good" );
  76. }
To copy to clipboard, switch view to plain text mode 

This codes tells three things.
1. buttonA push : thread executes. And label->setText calls with received data.
2. buttonB push : thread stops. And label->setText calls with "good"
3. if thread executes, it calls the member function of "test_widget".

There is no problem when starting QT Application. But when I press button 'A' and 'B' alternately, when the button 'A' is pressed at some point, the number is not updated. Then sometimes the program is killed.(Segment fault)
(The above code is a pseudocode, so it may not work. If you want the code for testing, I can provide it.)

I want to know the cause of this issue. Please help me.
Thanks.