调用堆栈中的访问冲突

时间:2019-01-30 18:35:24

标签: c++ visual-studio-2010 pointers access-violation

我收到此错误:Cts.exe中0x003f31b5的第一次机会异常:0xC0000005:访问冲突读取位置0xe1672514。

当我使用调用堆栈时,它会弹出该方法。 我特别评论了哪一行。

当我在程序运行时单击从调试中取消时,就会发生冲突。

在底部,我也包括了Cancel方法。

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;

}

}

这是取消方法:

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();

}

这是用C ++ Visual Studio 2010编码的。我认为可能有一些NULL指针之类的东西,但是我不确定。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

您的OnCancel正在取消分配内存,但未确保OnTimer仍在访问该内存。

在删除变量之前,请确保调用KillTimer(并确保OnTimer已完成)。