From: Anders Eriksson on 21 May 2007 02:53 Hello, I'm using a CMap<int,int,CString,LPCTSTR> for storing error messages which I get from a PLC as error codes. This works very well, except that when I exit the program it crashes in CPlex::FreeDataChain() with this error message: Unhandled exception at 0x78356c6e (mfc80ud.dll) in Marker.exe: 0xC0000005: Access violation reading location 0x00000002. If I run boundschecker I get an error on the SetAt saying: Pointer arithmetic range error My code look like this // MarkerView.h CMap<int,int,CString,LPCTSTR> m_larmtextMap; //MarkerView.cpp int kod; CString tail; // in a loop where I read a text file filling kod and tail with values // m_larmtextMap.SetAt(kod,tail); // // end loop I then use the map like this: BOOL brc = m_larmtextMap.Lookup(kod,txt); and in CMarkerView::Destroy() I do m_larmtextMap.RemoveAll(); The call stack is: > mfc80ud.dll!CPlex::FreeDataChain() Line 42 + 0x3 bytes C++ mfc80ud.dll!CPtrList::RemoveAll() Line 48 C++ mfc80ud.dll!CPtrList::FreeNode(CPtrList::CNode * pNode=0x003dd608) Line 123 C++ mfc80ud.dll!CPtrList::RemoveAt(__POSITION * position=0x003dd608) Line 324 C++ mfc80ud.dll!CFrameWnd::RemoveControlBar(CControlBar * pBar=0x003db44c) Line 875 C++ mfc80ud.dll!CControlBar::OnDestroy() Line 586 C++ mfc80ud.dll!CWnd::OnWndMsg(unsigned int message=2, unsigned int wParam=0, long lParam=0, long * pResult=0x0012e970) Line 2028 C++ mfc80ud.dll!CWnd::WindowProc(unsigned int message=2, unsigned int wParam=0, long lParam=0) Line 1741 + 0x20 bytes C++ mfc80ud.dll!CControlBar::WindowProc(unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 504 + 0x14 bytes C++ mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x003db44c, HWND__ * hWnd=0x000212da, unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 240 + 0x1c bytes C++ mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x000212da, unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 389 C++ mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x000212da, unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 407 + 0x15 bytes C++ user32.dll!7e418734() [Frames below may be incorrect and/or missing, no symbols loaded for user32.dll] user32.dll!7e418816() mfc80ud.dll!CThreadSlotData::GetThreadValue(int nSlot=1599744) Line 265 C++ user32.dll!7e41b4c0() user32.dll!7e41b50c() ntdll.dll!7c90eae3() user32.dll!7e41daf6() mfc80ud.dll!CWnd::DestroyWindow() Line 993 + 0xd bytes C++ mfc80ud.dll!CControlBar::DestroyWindow() Line 598 C++ mfc80ud.dll!CFrameWnd::DestroyDockBars() Line 1618 C++ mfc80ud.dll!CFrameWnd::OnDestroy() Line 852 C++ Marker.exe!CMainFrame::OnDestroy() Line 110 C++ mfc80ud.dll!CWnd::OnWndMsg(unsigned int message=2, unsigned int wParam=0, long lParam=0, long * pResult=0x0012eec4) Line 2028 C++ mfc80ud.dll!CWnd::WindowProc(unsigned int message=2, unsigned int wParam=0, long lParam=0) Line 1741 + 0x20 bytes C++ mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x003db378, HWND__ * hWnd=0x0002132c, unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 240 + 0x1c bytes C++ mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x0002132c, unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 389 C++ mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x0002132c, unsigned int nMsg=2, unsigned int wParam=0, long lParam=0) Line 407 + 0x15 bytes C++ user32.dll!7e418734() user32.dll!7e418816() mfc80ud.dll!CThreadSlotData::GetThreadValue(int nSlot=1599744) Line 265 C++ user32.dll!7e41b4c0() user32.dll!7e41b50c() ntdll.dll!7c90eae3() user32.dll!7e41daf6() mfc80ud.dll!CWnd::DestroyWindow() Line 993 + 0xd bytes C++ mfc80ud.dll!CDocument::OnCloseDocument() Line 744 C++ mfc80ud.dll!CDocTemplate::CloseAllDocuments(int __formal=0) Line 355 C++ mfc80ud.dll!CDocManager::CloseAllDocuments(int bEndSession=0) Line 588 C++ mfc80ud.dll!CWinApp::CloseAllDocuments(int bEndSession=0) Line 90 C++ mfc80ud.dll!CFrameWnd::OnClose() Line 798 C++ mfc80ud.dll!CWnd::OnWndMsg(unsigned int message=16, unsigned int wParam=0, long lParam=0, long * pResult=0x0012f2cc) Line 2028 C++ mfc80ud.dll!CWnd::WindowProc(unsigned int message=16, unsigned int wParam=0, long lParam=0) Line 1741 + 0x20 bytes C++ mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x003db378, HWND__ * hWnd=0x0002132c, unsigned int nMsg=16, unsigned int wParam=0, long lParam=0) Line 240 + 0x1c bytes C++ mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x0002132c, unsigned int nMsg=16, unsigned int wParam=0, long lParam=0) Line 389 C++ mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x0002132c, unsigned int nMsg=16, unsigned int wParam=0, long lParam=0) Line 407 + 0x15 bytes C++ user32.dll!7e418734() user32.dll!7e418816() user32.dll!7e41b4c0() user32.dll!7e41b50c() ntdll.dll!7c90eae3() user32.dll!7e4194be() user32.dll!7e41b42d() user32.dll!7e4184fc() user32.dll!7e41ba0e() user32.dll!7e4184fc() user32.dll!7e4185a4() user32.dll!7e41b3f9() uxtheme.dll!5ad73c20() uxtheme.dll!5ad8e300() uxtheme.dll!5ad71ac7() uxtheme.dll!5ad71b3d() uxtheme.dll!5ad8e2d5() user32.dll!7e41bb15() user32.dll!7e418734() user32.dll!7e418816() user32.dll!7e41c63f() user32.dll!7e41c665() mfc80ud.dll!CWnd::DefWindowProcW(unsigned int nMsg=274, unsigned int wParam=61536, long lParam=3867868) Line 1029 + 0x20 bytes C++ mfc80ud.dll!CWnd::Default() Line 274 C++ mfc80ud.dll!CWnd::OnSysCommand(unsigned int __formal=61536, unsigned int __formal=61536) Line 460 + 0xf bytes C++ mfc80ud.dll!CFrameWnd::OnSysCommand(unsigned int nID=61536, long lParam=3867868) Line 1042 C++ mfc80ud.dll!CWnd::OnWndMsg(unsigned int message=274, unsigned int wParam=61536, long lParam=3867868, long * pResult=0x0012f8cc) Line 2056 C++ mfc80ud.dll!CWnd::WindowProc(unsigned int message=274, unsigned int wParam=61536, long lParam=3867868) Line 1741 + 0x20 bytes C++ mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x003db378, HWND__ * hWnd=0x0002132c, unsigned int nMsg=274, unsigned int wParam=61536, long lParam=3867868) Line 240 + 0x1c bytes C++ mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x0002132c, unsigned int nMsg=274, unsigned int wParam=61536, long lParam=3867868) Line 389 C++ mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x0002132c, unsigned int nMsg=274, unsigned int wParam=61536, long lParam=3867868) Line 407 + 0x15 bytes C++ user32.dll!7e418734() user32.dll!7e418816() user32.dll!7e41b89b() user32.dll!7e41b903() uxtheme.dll!5ad9881f() uxtheme.dll!5ad71ac7() uxtheme.dll!5ad71b3d() user32.dll!7e41bb15() user32.dll!7e418734() user32.dll!7e418816() user32.dll!7e41c63f() user32.dll!7e41c665() mfc80ud.dll!CWnd::DefWindowProcW(unsigned int nMsg=161, unsigned int wParam=20, long lParam=3867868) Line 1029 + 0x20 bytes C++ mfc80ud.dll!CWnd::WindowProc(unsigned int message=161, unsigned int wParam=20, long lParam=3867868) Line 1742 + 0x1c bytes C++ mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x003db378, HWND__ * hWnd=0x0002132c, unsigned int nMsg=161, unsigned int wParam=20, long lParam=3867868) Line 240 + 0x1c bytes C++ mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x0002132c, unsigned int nMsg=161, unsigned int wParam=20, long lParam=3867868) Line 389 C++ mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x0002132c, unsigned int nMsg=161, unsigned int wParam=20, long lParam=3867868) Line 407 + 0x15 bytes C++ user32.dll!7e418734() user32.dll!7e418816() user32.dll!7e4189cd() user32.dll!7e418a10() mfc80ud.dll!AfxInternalPumpMessage() Line 183 C++ mfc80ud.dll!CWinThread::PumpMessage() Line 896 C++ mfc80ud.dll!CWinThread::Run() Line 625 + 0xd bytes C++ mfc80ud.dll!CWinApp::Run() Line 889 C++ mfc80ud.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, wchar_t * lpCmdLine=0x00020f94, int nCmdShow=1) Line 47 + 0xd bytes C++ Marker.exe!wWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, wchar_t * lpCmdLine=0x00020f94, int nCmdShow=1) Line 29 C++ Marker.exe!__tmainCRTStartup() Line 578 + 0x35 bytes C Marker.exe!wWinMainCRTStartup() Line 403 C kernel32.dll!7c816fd7() kernel32.dll!7c810665() // Anders -- English is not my first, or second, language so anything strange, or insulting, is due to the translation. Please correct me so I may improve my English!
From: MrAsm on 21 May 2007 04:13 On Mon, 21 May 2007 08:53:47 +0200, Anders Eriksson <andis59(a)gmail.com> wrote: >I'm using a CMap<int,int,CString,LPCTSTR> for storing error messages which >I get from a PLC as error codes. This works very well, except that when I >exit the program it crashes in CPlex::FreeDataChain() with this error >message: >My code look like this >// MarkerView.h >CMap<int,int,CString,LPCTSTR> m_larmtextMap; > >//MarkerView.cpp >int kod; >CString tail; >// in a loop where I read a text file filling kod and tail with values >// > m_larmtextMap.SetAt(kod,tail); >// >// end loop > >I then use the map like this: >BOOL brc = m_larmtextMap.Lookup(kod,txt); > >and in CMarkerView::Destroy() I do >m_larmtextMap.RemoveAll(); You might consider posting more significant portions of your code. I see no errors in the C++ statements you posted (I also did some similar tests with CMap, and I found no problems). MrAsm
From: MrAsm on 21 May 2007 05:27 On Mon, 21 May 2007 08:53:47 +0200, Anders Eriksson <andis59(a)gmail.com> wrote: >I'm using a CMap<int,int,CString,LPCTSTR> for storing error messages which >I get from a PLC as error codes. This works very well, except that when I >exit the program it crashes in CPlex::FreeDataChain() with this error >message: > >Unhandled exception at 0x78356c6e (mfc80ud.dll) in Marker.exe: 0xC0000005: >Access violation reading location 0x00000002. Another "plan of attack" to your problem could also be to use STL std::map instead of CMap. std::map is very easy to use (at least, its basic use), e.g.: // // Map int to CString // (note that you can mix STL std::map with MFC CString) // typedef std::map< int, CString > IntStringMap; IntStringMap data; // Add some data into the map; very easy with the // operator[] overload data[3] = _T("Hello"); data[10] = _T("All right"); // You can retrieve map data also using operator[]: Print( data[3] ); // Hello Print( data[10] ); // All right // Remove map data using map::clear method data.clear(); You might consider refactoring your code using std::map and see if the program crashes again. However, I believe that the crash is not CMap's fault, but it's a fault of other part of the code (even if it seems not clear basing on the C++ statements you posted.) MrAsm
From: Eric Lilja on 21 May 2007 05:38 Just be a bit careful with operator [] for std::map. If there's no element with the given key a new, default-constructed one, is inserted and a reference to it is returned. And in some cases you just want to know if an element is in the map without actually inserting as part of that check and then you use find(). MrAsm wrote: > On Mon, 21 May 2007 08:53:47 +0200, Anders Eriksson > <andis59(a)gmail.com> wrote: > >> I'm using a CMap<int,int,CString,LPCTSTR> for storing error messages which >> I get from a PLC as error codes. This works very well, except that when I >> exit the program it crashes in CPlex::FreeDataChain() with this error >> message: >> >> Unhandled exception at 0x78356c6e (mfc80ud.dll) in Marker.exe: 0xC0000005: >> Access violation reading location 0x00000002. > > Another "plan of attack" to your problem could also be to use STL > std::map instead of CMap. > > std::map is very easy to use (at least, its basic use), e.g.: > > // > // Map int to CString > // (note that you can mix STL std::map with MFC CString) > // > typedef std::map< int, CString > IntStringMap; > IntStringMap data; > > // Add some data into the map; very easy with the > // operator[] overload > data[3] = _T("Hello"); > data[10] = _T("All right"); > > // You can retrieve map data also using operator[]: > Print( data[3] ); // Hello > Print( data[10] ); // All right > > // Remove map data using map::clear method > data.clear(); > > You might consider refactoring your code using std::map and see if the > program crashes again. > > However, I believe that the crash is not CMap's fault, but it's a > fault of other part of the code (even if it seems not clear basing on > the C++ statements you posted.) > > MrAsm
From: MrAsm on 21 May 2007 05:57
On Mon, 21 May 2007 11:38:25 +0200, Eric Lilja <mindcoolerremoveme(a)gmail.com> wrote: >Just be a bit careful with operator [] for std::map. If there's no >element with the given key a new, default-constructed one, is inserted >and a reference to it is returned. And in some cases you just want to >know if an element is in the map without actually inserting as part of >that check and then you use find(). Yes, I know both this aspect of operator[] and find() method. But I was not going deep into the discussion because I saw no check for existing/non-existing elements with a given key in the OP's code. Of course, you're right that if the OP needs to check if a key is already in the map, he could use find method: if ( data.find( key ) != data.end() ) // ... key already in the map MrAsm |