Unhandled Exception on ProcessShellCommand()

My MFC app urps on startup with an:
"Unhandled Exception.at 0x7823eafb (mfc80d.dll).in MyApp.exe. 0xC0000005: Access violation reading location 0xfdfe3f55".

I need some help finding out why this occurs or some advice on how to find/fix the problem. It worked until I converted it from VC++ 6 to Visual Studio 2005, but now fails in both IDE's.

The error appears during MyApp startup when ProcessShellCommand() is called from within InitInstance(). The cmdinfo structure sends a 'FileNew' command with an empty document name and the framework goes to work building the Doc and the View (SDIDoc app). Debugging shows the error occurs during the part where the framework is trying to create the main window. More specifically, it looks like during the window creation a handle to something cannot be obtained.

The stack trace looks like:
- mfc80d.dll!CMapPtrToPtr::GetValueAt(void * key=0x000a05c4)  Line 188 + 0x9 bytes
- mfc80d.dll!CHandleMap::LookupPermanent(void * h=0x000a05c4)  Line 114 + 0x16 bytes
- mfc80d.dll!CWnd::FromHandlePermanent(HWND__ * hWnd=0x000a05c4)  Line 321 + 0xc bytes
- mfc80d.dll!_AfxCbtFilterHook(int code=3, unsigned int wParam=656836, long lParam=1241572)  Line 525 + 0x9 bytes
<<a bunch of calls to user32.dll and ntdll.dll delete here for brevity>>
- mfc80d.dll!AfxCtxCreateWindowExA(unsigned long dwExStyle=512, const char * lpClassName=0x006a1800, const char * lpWindowName=0x003acf20, unsigned long dwStyle=550436864, int X=238, int Y=0, int nWidth=923, int nHeight=1022, HWND__ * hWndParent=0x00000000, HMENU__ * hMenu=0x008f02e3, HINSTANCE__ * hInstance=0x00400000, void * lpParam=0x0012f9c8)  Line 438 + 0xa1 bytes
- mfc80d.dll!CWnd::CreateEx(unsigned long dwExStyle=0, const char * lpszClassName=0x00000000, const char * lpszWindowName=0x003acf20, unsigned long dwStyle=13598720, int x=-2147483648, int y=-2147483648, int nWidth=-2147483648, int nHeight=-2147483648, HWND__ * hWndParent=0x00000000, HMENU__ * nIDorHMenu=0x008f02e3, void * lpParam=0x0012f9c8)  Line 694 + 0x35 bytes
- mfc80d.dll!CFrameWnd::Create(const char * lpszClassName=0x00000000, const char * lpszWindowName=0x003acf20, unsigned long dwStyle=13598720, const tagRECT & rect={...}, CWnd * pParentWnd=0x00000000, const char * lpszMenuName=0x00000080, unsigned long dwExStyle=0, CCreateContext * pContext=0x0012f9c8)  Line 560 + 0x54 bytes
- mfc80d.dll!CFrameWnd::LoadFrame(unsigned int nIDResource=128, unsigned long dwDefaultStyle=13598720, CWnd * pParentWnd=0x00000000, CCreateContext * pContext=0x0012f9c8)  Line 692 + 0x35 bytes
- mfc80d.dll!CDocTemplate::CreateNewFrame(CDocument * pDoc=0x01450040, CFrameWnd * pOther=0x00000000)  Line 277 + 0x22 bytes
- mfc80d.dll!CSingleDocTemplate::OpenDocumentFile(const char * lpszPathName=0x00000000, int bMakeVisible=1)  Line 125 + 0x13 bytes
- mfc80d.dll!CDocManager::OnFileNew()  Line 848
- mfc80d.dll!CWinApp::OnFileNew()  Line 22
- mfc80d.dll!CWinApp::ProcessShellCommand(CCommandLineInfo & rCmdInfo={...})  Line 26

The actual exception occurs within CMapPtrToPtr::GetValue in the for loop. GetValue() looks like:

void* CMapPtrToPtr::GetValueAt(void* key) const
// find value (or return NULL -- NULL values not different as a result)

      if (m_pHashTable == NULL)
            return NULL;

      UINT nHash = HashKey(key) % m_nHashTableSize;

      // see if it exists
      CAssoc* pAssoc;
      for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)  //<--- *** error occurs here ***
            if (pAssoc->key == key)
                  return pAssoc->value;
      return NULL;

The value of m_pHashTable is 0xFDFDFDFD. Isn’t that the value used for a freed long pointer?
m_nHasTableSize is also 0xFDFDFDFD (4261281277) and the resultant nHash is 36950. It seems like the hash table is invalid at this point hence the error.

Who is Participating?
EE_AutoDeleterConnect With a Mentor Commented:
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

dnewtontxAuthor Commented:
I found the answer.

After adding the following code it worked:

      // InitCommonControlsEx() is required on Windows XP if an application
      // manifest specifies use of ComCtl32.dll version 6 or later to enable
      // visual styles.  Otherwise, any window creation will fail.
      InitCtrls.dwSize = sizeof(InitCtrls);
      // Set this to include all the common control classes you want to use
      // in your application.
      InitCtrls.dwICC = ICC_WIN95_CLASSES;

This was added to my InitInstance().
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.