'Why MFC dbgheap.c 1044 line runtime error occurs

An error sometimes occurs during program running. It doesn't happen every time.

enter image description here

I don't know why I get an error in this part. The length of strAckData is greater than 13

x_FWCommunity->sRevDataStr = strAckData.GetAt(13); '

User SourceCode

void CMLT2App::AckTotalFusingMsg(CString strAckData)
{
  char szLog[512];
  CString strCmd(""), strResult(""), strTemp(""), strLen("");
  char cResult;
  int nCmd = 0, nPos = 0, nDataLen = 0;

  strTemp = strAckData;
  nPos = strTemp.Find(0x02);
  strAckData = strTemp.Mid(nPos, strAckData.GetLength() - nPos);
  if (strAckData.GetAt(0) != 0x02) return; // Check STX
  if (strAckData.GetAt((strAckData.GetLength() - 1)) != 0x03) // Check ETX
    return;

  strCmd.Format("%s", strAckData.Mid(7, 2)); // Get Command;
  sscanf(strCmd, "%X", &nCmd); // String Convert to Hex
  cResult = strAckData.GetAt(13); // String Convert to Decimal    
  x_FWCommunity->sRevCmdStr = strCmd;

  strLen.Format("%s", strAckData.Mid(9, 4));
  sscanf(strLen, "%X", &nDataLen);
  // strCmd 는 0xBD
  if (strCmd == "FE" || strCmd == "5E")
  {
    x_FWCommunity->sRevDataStr = strAckData.Mid(13, nDataLen);
    // AfxMessageBox(x_FWCommunity->sRevDataStr);
  }
  else
    x_FWCommunity->sRevDataStr = strAckData.GetAt(13);    ← Error Point

    switch (nCmd)
    {
    case CMD_CTRL_FUSING_SYSTEM:
    case CMD_CTRL_FUSING_CONTROL_PTN:
    case CMD_CTRL_FUSING_PREDISP_PTN:

Call Stack Log

MSVCRTD! 102136a8()
MSVCRTD! 102135b1()
operator delete(void * 0x01a11f28) line 351 + 12 bytes
CString::FreeData() line 146 + 15 bytes
CString::Release() line 157
CString::AllocBeforeWrite(int 1) line 200
CString::AssignCopy(int 1, const char * 0x01e2e684) line 315
CString::operator=(char 48) line 88
CMLT2App::AckTotalFusingMsg(CString {"A2A100BD002300375000011320373000000630331000000C3"}) line 6029
CMLT2App::RS232C_ReceivedFusingInfo(unsigned char * 0x01e2eb8c) line 5412
ThreadWatchComm1(CPortController * 0x01514010) line 966
KERNEL32! 7c7db729()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source