我的程序在执行是出现了Unhandled exception in XXX.exe(USER32.DLL):0X0000005:Access Violation错误,但是并没有空指针,错误出在:
void CSerialPort::WriteToPort(char* string, UINT length)
{
assert(m_hComm != 0); memcpy(cSendBuffer+nSendLength, string, length); nSendLength += length;
// set event for write
SetEvent(m_hWriteEvent);
}
其中nSendLength是发送长度,cSendBuffer是发送缓冲区长度。这个函数本来没有错误,能正常运行,我的程序分为物理层,数据链路层。在物理层,该函数是没有错误的。可是我在数据链路层加上了
void TNetDLLEntityPPP::send(){
makeDataFrame(strlen(message),message,forsend);//构造数据帧
    packet(forsend);                                    //把数据打包
int len1=strlen(message);                      //message-存放消息的字符串
int len2=strlen(strframe);                     //strframe-存放帧的字符串
int i=0,j=0; 

TNetPHLEntityCom  *comforsend=new TNetPHLEntityCom;//物理端口类
do
{
        
comforsend->dataSend(message[i]) ;      //发送数据
i++;
}while(i<len1);
do
{
           comforsend->dataSend(strframe[j]) ;
j++;
}while(j<len2);
delete comforsend;
message[0]='\0';
}
并在一个Dialog的ONOK函数中加上void CDllSend::OnOK() 
{
// TODO: Add extra validation here
    UpdateData(TRUE);
    if(m_DllSend=="\0")
{
AfxMessageBox("发送消息不能为空!");
return;
}
    TNetDLLEntityPPP *forsending=new TNetDLLEntityPPP;
    strcpy(forsending->message,m_DllSend);
    forsending->send();
delete forsending;
CDialog::OnOK();
}后就出现了上面的错误,不知道是什么原因,我觉得错误很可能出在对dataSend的使用上,但是又不知使用错在哪。请大虾指点。非常感谢

解决方案 »

  1.   

    肯定是指针问题,指针访问越界或者是空指针。Unhandled exception in XXX.exe(USER32.DLL):0X0000005:Access Violation
    ====================================
    这个错误信息就是告诉你有指针尝试访问了0x0000005这个地址,根据Windows对进程的内存分配这段地址一般是属于NULL块,用于发现空指针访问错误的。具体的可以参考《Windows核心编程》中的关于内存的那一章节的说明!
      

  2.   

    把CDialog::OnOK();去掉,可能是析构的问题.