调用堆栈中的访问冲突

Access Violation in call stack

我收到此错误: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 指针或其他东西,但我不确定。任何帮助,将不胜感激。谢谢。

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

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