title: visual studio access violation reading location 0xc0000005
body: <p>I am receiving data in TCP in C++ using Qt library. I store the received packet in a <code>QByteArray</code>, but after reading the whole data, I face this error in debug. At the end, I try to clear the buffer, but I face this problem while trying to clear it too.</p>
  
  <p>Here is my code :  </p>
  
  <pre><code>void AvaNPortTester::scoket_readyRead()
  {
      ui.lineEdit_Sending_Status_-&gt;setText("Sent");
      ui.lineEdit_Sending_Status_-&gt;setStyleSheet("QLineEdit { background: rgb(50, 255, 50);  }");
      tcpSocket_data_buffer_.append(tcpSocket_-&gt;readAll());
  
      //qDebug() &lt;&lt; serialport_data_buffer_.size();
      //auto ddd = QString::number(tcpSocket_data_buffer_.size());// +" : " + tcpSocket_data_buffer_.toHex();
      //ui.lableSocketRead-&gt;setText(ddd);
      bool read_aain = false;
      QByteArray dummy(int(1446), Qt::Initialization::Uninitialized);
      int reminded_data = 0;
      int dummy_size = 0;
      int frame_size = 0;
      int l_size = 0;     
  
      int total_size_rcvd = tcpSocket_data_buffer_.size();
      //int total_size_rcvd_b = total_size_rcvd_b;
      int temp = 0;
      while (total_size_rcvd != 0)
      {
          if(total_size_rcvd != 0){
          auto packet = tcpSocket_data_buffer_.mid(0, 1446);
          auto rem = tcpSocket_data_buffer_.mid(1446);//****1146
  
          tcpSocket_data_buffer_ = rem;
  
          QDataStream streamdata(packet);
  
  
          uint8_t  Sync_Header[3];
          auto ss = streamdata.readRawData((char*)&amp;Sync_Header, 3);
  
          uint8_t  Total_size[2];
          ss = streamdata.readRawData((char*)&amp;Total_size, 2);
          int t_size = Total_size[0] * 256 + Total_size[1];
  
  
          uint8_t  Reserved[2];
          ss = streamdata.readRawData((char*)&amp;Reserved, 2);
  
          frame_size = t_size - 2;
          reminded_data = t_size - 2;
          while (frame_size != 0)
          {
  
              uint8_t  portid;
              ss = streamdata.readRawData((char*)&amp;portid, 1);
  
              //ui.lineEdit_FileSize-&gt;setText(QString::number(fileSend_2Ser-&gt;size()));
  
              uint8_t  ProtocolID;
              ss = streamdata.readRawData((char*)&amp;ProtocolID, 1);
  
              uint8_t  MoreFragmentFlag;
              ss = streamdata.readRawData((char*)&amp;MoreFragmentFlag, 1);
  
              uint8_t  Seq;
              ss = streamdata.readRawData((char*)&amp;Seq, 1);
  
              uint8_t  size[2];
              ss = streamdata.readRawData((char*)&amp;size, 2);
              l_size = size[0] * 256 + size[1];
  
              if (packet_flags.Ser2Eth.packet_started[portid] == false) {
                  uint8_t  DDCMP_Header[14];
                  ss = streamdata.readRawData((char*)&amp;DDCMP_Header, 14);
                  packet_flags.Ser2Eth.protocol_payload_size[portid] = DDCMP_Header[7] + 256 * DDCMP_Header[8];
                  temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
                  packet_flags.Ser2Eth.packet_started[portid] = true;
              }
              QByteArray ddcmp_datap(int(l_size), Qt::Initialization::Uninitialized);
              streamdata.readRawData(ddcmp_datap.data(), l_size - 14);
              if ((pre_more_frag == 0) &amp;&amp; (MoreFragmentFlag == 0)) {
                  packet_flags.Ser2Eth.packet_ended[portid] = true;
                  packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size;
                  temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
              }
              else if ((pre_more_frag == 0) &amp;&amp; (MoreFragmentFlag == 1)) {
                  packet_flags.Ser2Eth.packet_ended[portid] = false;
                  packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size + 16;
                  temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
              }
              else if ((pre_more_frag == 1) &amp;&amp; (MoreFragmentFlag == 1)) {
                  packet_flags.Ser2Eth.packet_ended[portid] = false;
                  packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
                  temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
              }
              else if ((pre_more_frag == 1) &amp;&amp; (MoreFragmentFlag == 0)) {
                  packet_flags.Ser2Eth.packet_ended[portid] = true;
                  packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
                  temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
              }
  
  
              if (MoreFragmentFlag == 1) {
                  pre_more_frag = 1;
              }
              else {
                  pre_more_frag = 0;
              }
              int ff = 0;
              if (packet_flags.Ser2Eth.packet_ended[portid] == true) {
                  packet_flags.Ser2Eth.packet_started[portid] = false;
                  packet_flags.Ser2Eth.packet_started[portid] = false;
                  set_port_id_flag(portid, packet_flags.Ser2Eth.protocol_payload_size[portid], ProtocolID);
                  pre_more_frag = 0;
              }
  
              reminded_data = reminded_data - 6 - l_size;
              //ui.lableSocketRead-&gt;setText(ddcmp_datap.toHex());
  
              frame_size = frame_size - l_size - 6;
          }//end of while (frame_size != 0)
  
          uint8_t sync_footer[3];
          streamdata.readRawData((char *)&amp;sync_footer, 3);
          dummy_size = 1446 - t_size - 8;
          uint8_t dummy_data[1000];
          streamdata.readRawData((char *)&amp;dummy_data, dummy_size);
          total_size_rcvd = total_size_rcvd - 1446;
          if (total_size_rcvd == 0) {
              tcpSocket_data_buffer_.clear();
          }
  
      } //end of if
      }//end of while()
  }
  </code></pre>