Sorry Jacek, I know that I should find out by myself but I don't manage to ... so I need some more help please.

I have modified the source code of my CSerialDataFrame in order to make a deep copy when I call the copy constructor, the equal operator but also the setSerialDataFrameValue. Here I am sure that the copy is deep.

I have analysed my code and I don't know what could be wrong but I'm sure there is something wrong ... it is starting to drive me crazy

First, the CSerialDataDecoder code looks fine
Qt Code:
  1. void CSerialDataFrameDecoder::run()
  2. {
  3. int bytesToRead = -1;
  4. Q_LONG bytesRead = 0;
  5.  
  6. int startSequenceIndex = -1;
  7. int stopSequenceIndex = -1;
  8.  
  9. QString buffer("");
  10. QString frame("");
  11.  
  12. while( bMustDecode == true )
  13. {
  14. msleep(1);
  15. // TO DO
  16. // ... read the com port buffer
  17. // ... decode frames
  18. // ... trigger a custom events each time a complete frame is decoded
  19. if( comPort != 0 )
  20. {
  21. if( comPort->isOpen() )
  22. {
  23. // Get the number of bytes to read
  24. bytesToRead = comPort->bytesWaiting();
  25.  
  26. if( bytesToRead != 0 )
  27. {
  28. // Here we can do some stuff
  29. char* buffData = 0;
  30. buffData = new char[bytesToRead+1];
  31.  
  32. bytesRead = comPort->readBlock(buffData, bytesToRead);
  33. buffData[bytesRead] = '\0';
  34. comPort->flush();
  35.  
  36. buffer = buffData;
  37.  
  38. while( (buffer.length() != 0) && (bMustDecode == true) )
  39. {
  40. msleep(1);
  41.  
  42. startSequenceIndex = buffer.find(startSequence, 0, false);
  43. stopSequenceIndex = buffer.find(stopSequence, 0, false);
  44. // Check previous indexes to determine if the frame is complete or not
  45. // CASE 1 : start and stop indexex are found, stop index is greather than start index
  46. // !!! A complete frame can be extracted !!!
  47.  
  48. // CASE 2 and CASE 5 : stop index is found but not start index
  49. // !!! An incomplete frame can be extracted, its beginning is missing !!!
  50.  
  51. // CASE 3 : start index is found but not stop
  52. // !!! An incomplete frame can be extracted, its ending is missing
  53.  
  54. // CASE 4 : start index neither stop are found
  55. // !!! An incomplete frame can be extracted, its beginning and ending are missing
  56.  
  57. // CASE 1
  58. if( (startSequenceIndex == 0) && (stopSequenceIndex != -1) && (stopSequenceIndex>startSequenceIndex) )
  59. {
  60. frame = buffer.mid(startSequenceIndex, (stopSequenceIndex-startSequenceIndex)+1);
  61. buffer.remove(0, stopSequenceIndex+stopSequence.length());
  62.  
  63. CSerialDataFrame aFrame;
  64. aFrame.setSerialDataFrameValue(frame);
  65. aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusComplete);
  66. this->serialDataFrameDecoded(aFrame);
  67. }
  68. // CASE 2
  69. else if( (startSequenceIndex == -1) && (stopSequenceIndex != -1) )
  70. {
  71. frame = buffer.left(stopSequenceIndex+1);
  72. buffer.remove(0, stopSequenceIndex+stopSequence.length());
  73.  
  74. CSerialDataFrame aFrame;
  75. aFrame.setSerialDataFrameValue(frame);
  76. aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheEndingPart);
  77. this->serialDataFrameDecoded(aFrame);
  78. }
  79. // CASE 3
  80. else if( (startSequenceIndex != -1) && (stopSequenceIndex == -1) )
  81. {
  82. frame = buffer.mid(0, buffer.length());
  83. buffer.remove(0, buffer.length());
  84.  
  85. CSerialDataFrame aFrame;
  86. aFrame.setSerialDataFrameValue(frame);
  87. aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheBeginningPart);
  88. this->serialDataFrameDecoded(aFrame);
  89. }
  90. // CASE 4
  91. else if( (startSequenceIndex == -1) && (stopSequenceIndex == -1) )
  92. {
  93. frame = buffer;
  94. buffer.remove(frame);
  95.  
  96. CSerialDataFrame aFrame;
  97. aFrame.setSerialDataFrameValue(frame);
  98. aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheMiddlePart);
  99. this->serialDataFrameDecoded(aFrame);
  100. }
  101. // CASE 5
  102. else if( (startSequenceIndex != -1) && (stopSequenceIndex != -1) && (stopSequenceIndex<startSequenceIndex) )
  103. {
  104. frame = buffer.left(stopSequenceIndex+1);
  105. buffer.remove(0, stopSequenceIndex+stopSequence.length());
  106.  
  107. CSerialDataFrame aFrame;
  108. aFrame.setSerialDataFrameValue(frame);
  109. aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheEndingPart);
  110. this->serialDataFrameDecoded(aFrame);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118.  
  119.  
  120. void CSerialDataFrameDecoder::serialDataFrameDecoded(const CSerialDataFrame& frame)
  121. {
  122. // Allocate some memory for the data
  123. CSerialDataFrame* data = 0;
  124. data = new CSerialDataFrame(frame);
  125. // The allocated memory for the data MUST by freed by the receiver
  126.  
  127. // Allocate some memory for the custom event
  128. QCustomEvent* ce = 0;
  129. ce = new QCustomEvent(1002);
  130. ce->setData(data);
  131.  
  132. // Send the event
  133. QApplication::postEvent(objectOwner, ce);
  134. // The allocated memory for the custum event is freed by Qt so the receiver neither the sender MUST NOT free it
  135. }
To copy to clipboard, switch view to plain text mode 

In my opinion its behaviour is correct and match with the code. It just flush the buffer, reads data from the Win_QextSerialPort and add a null terminating char. Maybe should I make a deep copy of the buffer content and store it into my QString variable buffer ? After that it processes the buffer variable and looks for a start and stop sequence char, extract the data and then update the buffer variable (it removes the extracted data). After extracting the serial data frame, it allocated some memory for a such a frame, make a copy of it and then send this frame using a custom event to the owner of the CSerialDataFrameDecoder which is the CSerialPort object. At this point should I do something else in that code ? (Other deep copy ?, mutex ?).