Search code examples
c++visual-studio-2010pointersaccess-violation

Access Violation in call stack


I get this error: First-chance exception at 0x003f31b5 in Cts.exe: 0xC0000005: Access violation reading location 0xe1672514.

When I use the call stack, it spits out to this method. I commented which line specifically.

When I click cancel from debugging when the program runs, the violation happens.

At the bottom, I have included my Cancel method as well.

void CInpINS::OnTimer(UINT nIDEvent) 
{

  int i,j, totalbytes;
  bool bfilefnd = false;


  CConvb Convb;
  CString tmp;

  for (i = 0; i < (int) m_nNumMsgs; i++) {
      m_pBDF[i]->m_numrecs = m_pIDF[i]->m_numrecs;

      for (j = 0; j < MAXBYTECNT; j++) {
                OutBytes[j] = 0;
      }

        // set first 5 words 
      OutBytes[1] = m_nSelectedMsgNum[i];

      OutBytes[3] = (int)m_pIDF[i]->IDFFields[m_pIDF[i]->m_numrecs-1].ebyte/2+6; // THIS LINE SPECIFICALLY

      CConvb Convb;

      if (i == 0) m_dTimeofTransmission += m_nRate;
      tmp.Format("%20.0f",m_dTimeofTransmission);

      Convb.CONV_Timetag_to_Bytes(tmp, OutBytes[4], OutBytes[5],
                              OutBytes[6], OutBytes[7],
                              OutBytes[8], OutBytes[9],
                              OutBytes[10], OutBytes[11]);

        // start at 11 because byte 0 and 1 are input or output msg, then bytes 2 and 3 are word count
        // bytes 4 through 11 are gps time
        for (j = 0; j < m_pBDF[i]->m_numrecs; j++) {
            if ((j == 0)||(j == 1))  
            {
                Convb.ConvFld(tmp,
                    m_pIDF[i]->IDFFields[j].bbyte+9,
                    m_pIDF[i]->IDFFields[j].ebyte+9,
                    m_pIDF[i]->IDFFields[j].bbit,
                    m_pIDF[i]->IDFFields[j].ebit,
                    m_pIDF[i]->IDFFields[j].dtype,
                    m_pIDF[i]->IDFFields[j].Desc,OutBytes);

            }
            else
            {
                Convb.ConvFld(m_pBDF[i]->BDFFields[j],
                    m_pIDF[i]->IDFFields[j].bbyte+9,
                    m_pIDF[i]->IDFFields[j].ebyte+9,
                    m_pIDF[i]->IDFFields[j].bbit,
                    m_pIDF[i]->IDFFields[j].ebit,
                    m_pIDF[i]->IDFFields[j].dtype,
                    m_pIDF[i]->IDFFields[j].Desc,OutBytes);

            }
        }

        totalbytes = OutBytes[3];
        m_pDoc->sendmsg(totalbytes, false, OutBytes);
        tmp.Format("Sent Message");
        AddToListBox(tmp);
        UpdateData(false);
        m_nNumSent +=1;

}

}

Here is the cancel method:

void CInpINS::OnCancel() 
    {
if (m_bSetIDF) 
{
    for (int i = 0; i < (int) m_nNumMsgs; i++) {
        delete m_pIDF[i];
        delete m_pIDFCustm[i];
        delete m_pBDF[i];   
    }
    m_bSetIDF = false;
}

AfxGetMainWnd()->PostMessage(WM_GOODBYEINPINS, IDOK);

CDialog::OnCancel();

}

This is coded in C++ Visual Studio 2010. I think there may be some NULL pointers or something but I am not sure. Any help would be appreciated. Thank you.


Solution

  • Your OnCancel is de-allocating memory without making sure that OnTimer isn't still accessing that memory.

    Make sure to call KillTimer (and ensure that OnTimer has finished) before deleting the variables.