Unhandled Exception-0xC0000005:Access Violation
-
My program creates an object of CList, the nodes of which contains CMessage class object. The file has two functions, Write2List() and ReadFromList().After executing the program,i get no errors. But if i debug the program, i get "Unhandled Exception-0xC0000005:Access Violation" error after coming out of the ReadFromList(). Please help. The code is as follows include #include #include class CMessage { public: unsigned char m_cnt, m_mid; public: //constructor CMessage(); CMessage(unsigned char nCnt, unsigned char nMid, unsigned char* pdata); //destructor ~CMessage(); void SetValues(unsigned char nCnt, unsigned char nMid, unsigned char* pdata); int GetData(unsigned char* pdata); private: unsigned char m_data[4]; //body of the message }; CMessage::CMessage() { } CMessage::CMessage(unsigned char nCnt, unsigned char nMid, unsigned char* pData) : m_cnt(nCnt), m_mid(nMid) { for(int i=0; i <= ( m_cnt - 2); i++) m_data[i] = *pData++; } CMessage::~CMessage() { // m_cnt = '\0'; // m_mid = '\0'; // memcpy(m_data,'\0',16); } void CMessage::SetValues(unsigned char nCnt, unsigned char nMid, unsigned char* pdata) { m_cnt = nCnt; m_mid = nMid; for(int i=0; i <= ( m_cnt - 2); i++) m_data[i] = *pdata++; } int CMessage::GetData(unsigned char* pdata) { for(int i=0; i <= ( m_cnt - 2 ); i++) *pdata++ = m_data[i]; return 0; }//end of class CMessage //typedef for our list template. //Type is CMessage CList myList; void Write2List()//CMessage* pMessage1) { unsigned char data[4]; data[0] = 0x01; data[1] = 0x02; data[2] = 0x00; data[3] = 0x04; //first time of Initialisation CMessage* pMessage = new CMessage(); //setting values thru the function. pMessage->m_cnt = 0x40; pMessage->m_mid = 0x30; pMessage->SetValues(0x20,0x41,data); myList.AddTail(*pMessage); } void ReadFromList() { unsigned char data[10], mCnt,mMid; CMessage pMess; while(myList.GetCount() != 1) { pMess = myList.GetAt(myList.GetHeadPosition()); mCnt = pMess.m_cnt; mMid = pMess.m_mid; pMess.GetData(data); myList.RemoveHead(); } cout<<"abc"<
-
My program creates an object of CList, the nodes of which contains CMessage class object. The file has two functions, Write2List() and ReadFromList().After executing the program,i get no errors. But if i debug the program, i get "Unhandled Exception-0xC0000005:Access Violation" error after coming out of the ReadFromList(). Please help. The code is as follows include #include #include class CMessage { public: unsigned char m_cnt, m_mid; public: //constructor CMessage(); CMessage(unsigned char nCnt, unsigned char nMid, unsigned char* pdata); //destructor ~CMessage(); void SetValues(unsigned char nCnt, unsigned char nMid, unsigned char* pdata); int GetData(unsigned char* pdata); private: unsigned char m_data[4]; //body of the message }; CMessage::CMessage() { } CMessage::CMessage(unsigned char nCnt, unsigned char nMid, unsigned char* pData) : m_cnt(nCnt), m_mid(nMid) { for(int i=0; i <= ( m_cnt - 2); i++) m_data[i] = *pData++; } CMessage::~CMessage() { // m_cnt = '\0'; // m_mid = '\0'; // memcpy(m_data,'\0',16); } void CMessage::SetValues(unsigned char nCnt, unsigned char nMid, unsigned char* pdata) { m_cnt = nCnt; m_mid = nMid; for(int i=0; i <= ( m_cnt - 2); i++) m_data[i] = *pdata++; } int CMessage::GetData(unsigned char* pdata) { for(int i=0; i <= ( m_cnt - 2 ); i++) *pdata++ = m_data[i]; return 0; }//end of class CMessage //typedef for our list template. //Type is CMessage CList myList; void Write2List()//CMessage* pMessage1) { unsigned char data[4]; data[0] = 0x01; data[1] = 0x02; data[2] = 0x00; data[3] = 0x04; //first time of Initialisation CMessage* pMessage = new CMessage(); //setting values thru the function. pMessage->m_cnt = 0x40; pMessage->m_mid = 0x30; pMessage->SetValues(0x20,0x41,data); myList.AddTail(*pMessage); } void ReadFromList() { unsigned char data[10], mCnt,mMid; CMessage pMess; while(myList.GetCount() != 1) { pMess = myList.GetAt(myList.GetHeadPosition()); mCnt = pMess.m_cnt; mMid = pMess.m_mid; pMess.GetData(data); myList.RemoveHead(); } cout<<"abc"<
pMessage->SetValues(0x20,0x41,data);
This line in
Write2List
seems to indicate that data is 0x20-2=20 bytes long, yet it contains only 4 bytes. This might be causing the error. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
pMessage->SetValues(0x20,0x41,data);
This line in
Write2List
seems to indicate that data is 0x20-2=20 bytes long, yet it contains only 4 bytes. This might be causing the error. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
As an aside, it would make your life a lot easier if you use STL for your collection classes inside of the MFC collection junk.