This project has moved. For the latest updates, please go here.

Crash in VisualLeakDetector::reportLeaks

Oct 21, 2013 at 4:31 PM
Hi,

I have a crash in his function
SIZE_T VisualLeakDetector::reportLeaks (heapinfo_t* heapinfo, Set<blockinfo_t*> &aggregatedLeaks)   
    
    for (BlockMap::Iterator blockit = blockmap->begin(); blockit != blockmap->end(); ++blockit)
    {
        // Found a block which is still in the BlockMap. We've identified a
        // potential memory leak.
        LPCVOID block = (*blockit).first;
        
        ...
        
            crtdbgblockheader_t* crtheader = (crtdbgblockheader_t*)block;
            if (CRT_USE_TYPE(crtheader->use) == CRT_USE_IGNORE ||           <-- ACCESS VIOLATION
                CRT_USE_TYPE(crtheader->use) == CRT_USE_FREE ||
                CRT_USE_TYPE(crtheader->use) == CRT_USE_INTERNAL)
            {
                // This block is marked as being used internally by the CRT.
                // The CRT will free the block after VLD is destroyed.
                continue;
            }       
    
            address = CRTDBGBLOCKDATA(block);   
`           
What is happening is that crtheader contains only 16 bytes. The code expects to find a crtdbgblockheader_t structure folowed by memory.

This memory is not allocated via vldnew that prepends the block with a vldblockheader_t structure but trough
    vld_x64.dll!VisualLeakDetector::mapBlock(void * heap, const void * mem, unsigned __int64 size, bool crtalloc=false, CallStack * * & ppcallstack) Line 1031  C++
    vld_x64.dll!VisualLeakDetector::AllocateHeap(tls_t * tls, void * heap, void * block, unsigned __int64 size) Line 1586   C++
>   vld_x64.dll!VisualLeakDetector::_HeapAlloc(void * heap, unsigned long flags, unsigned __int64 size) Line 1573   C++
    Test.exe!__InitializeThunkPool() Line 132   C++
    Test.exe!ATL::__AllocStdCallThunk() Line 195    C++
    Test.exe!ATL::_stdcallthunk::operator new(unsigned __int64 __formal) Line 103   C++
that simply adds the memory to block blockmap
    VOID VisualLeakDetector::mapBlock (HANDLE heap, LPCVOID mem, SIZE_T size, bool crtalloc, CallStack **&ppcallstack)

    BlockMap::Iterator blockit = blockmap->insert(mem, blockinfo);
where mem is the real memory block allocated in `VisualLeakDetector::_HeapAlloc`

and does not set `VLD_HEAP_CRT_DBG` because __crtalloc=false__

The problem seems to be that nextcall to mapBlock for the same heap comes with crtalloc = true from this location
>   vld_x64.dll!VisualLeakDetector::mapBlock(void * heap, const void * mem, unsigned __int64 size, bool crtalloc = true, CallStack * * & ppcallstack) Line 1031 C++
    vld_x64.dll!VisualLeakDetector::AllocateHeap(tls_t * tls, void * heap, void * block, unsigned __int64 size) Line 1586   C++
    vld_x64.dll!VisualLeakDetector::_HeapAlloc(void * heap, unsigned long flags, unsigned __int64 size) Line 1573   C++
    msvcr110d.dll!_heap_alloc_base(unsigned __int64 size) Line 57   C
    msvcr110d.dll!_heap_alloc_dbg_impl(unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 431 C++
    msvcr110d.dll!_nh_malloc_dbg_impl(unsigned __int64 nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239  C++
    msvcr110d.dll!_nh_malloc_dbg(unsigned __int64 nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302    C++
    msvcr110d.dll!_malloc_dbg(unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine) Line 160   C++
    msvcr110d.dll!realloc_help(void * pUserData, unsigned __int64 * pnNewSize, int nBlockUse, const char * szFileName, int nLine, int fRealloc) Line 705    C++
    msvcr110d.dll!_realloc_dbg(void * pUserData, unsigned __int64 nNewSize, int nBlockUse, const char * szFileName, int nLine) Line 1040    C++
    msvcr110d.dll!_recalloc_dbg(void * memblock, unsigned __int64 count, unsigned __int64 size, int nBlockUse, const char * szFileName, int nLine) Line 1107    C++
    msvcr110d.dll!_recalloc(void * memblock, unsigned __int64 count, unsigned __int64 size) Line 987    C++
    vld_x64.dll!VisualLeakDetector::__recalloc(void * (void *, unsigned __int64, unsigned __int64) * precalloc, context_t & context, bool debugRuntime, void * mem, unsigned __int64 num, unsigned __int64 size) Line 272   C++
    vld_x64.dll!CrtMfcPatch<110,1>::crtd__recalloc(void * mem, unsigned __int64 num, unsigned __int64 size) Line 459    C++
    Test.exe!ATL::CSimpleArray<unsigned short,ATL::CSimpleArrayEqualHelper<unsigned short> >::Add(const unsigned short & t) Line 178    C++
    Test.exe!ATL::AtlWinModuleRegisterClassExW(ATL::_ATL_WIN_MODULE70 * pWinModule, const tagWNDCLASSEXW * lpwc) Line 5215  C++
    Test.exe!ATL::AtlModuleRegisterWndClassInfoParamW::RegisterClassExW(ATL::_ATL_WIN_MODULE70 * pWinModule, const tagWNDCLASSEXW * lpwcx) Line 5185    C++
    Test.exe!ATL::AtlModuleRegisterWndClassInfoT<ATL::AtlModuleRegisterWndClassInfoParamW>(ATL::_ATL_BASE_MODULE70 * pBaseModule, ATL::_ATL_WIN_MODULE70 * pWinModule, ATL::_ATL_WNDCLASSINFOW * p, __int64 (HWND__ *, unsigned int, unsigned __int64, __int64) * * pProc, ATL::AtlModuleRegisterWndClassInfoParamW __formal) Line 5290 C++
    Test.exe!ATL::AtlWinModuleRegisterWndClassInfoW(ATL::_ATL_WIN_MODULE70 * pWinModule, ATL::_ATL_BASE_MODULE70 * pBaseModule, ATL::_ATL_WNDCLASSINFOW * p, __int64 (HWND__ *, unsigned int, unsigned __int64, __int64) * * pProc) Line 5325   C++
    Test.exe!ATL::_ATL_WNDCLASSINFOW::Register(__int64 (HWND__ *, unsigned int, unsigned __int64, __int64) * * p) Line 328  C++
    Test.exe!ATL::CWindowImpl<WTL::CCommandBarCtrl,WTL::CCommandBarCtrlBase,ATL::CWinTraits<1442840576,0> >::Create(HWND__ * hWndParent, ATL::_U_RECT rect, const wchar_t * szWindowName, unsigned long dwStyle, unsigned long dwExStyle, ATL::_U_MENUorID MenuOrID, void * lpCreateParam) Line 3666    C++
    Test.exe!WTL::CCommandBarCtrlImpl<WTL::CCommandBarCtrl,WTL::CCommandBarCtrlBase,ATL::CWinTraits<1442840576,0> >::Create(HWND__ * hWndParent, tagRECT & rcPos, const wchar_t * szWindowName, unsigned long dwStyle, unsigned long dwExStyle, unsigned int nID, void * lpCreateParam) Line 466    C++
    Test.exe!CMainFrame::OnCreate(tagCREATESTRUCTW * lpCreateStruct) Line 252   C++
    Test.exe!CMainFrame::_ProcessWindowMessage(HWND__ * hWnd, unsigned int uMsg, unsigned __int64 wParam, __int64 lParam, __int64 & lResult, unsigned long dwMsgMapID) Line 169 C++
    Test.exe!CMainFrame::ProcessWindowMessage(HWND__ * hWnd, unsigned int uMsg, unsigned __int64 wParam, __int64 lParam, __int64 & lResult, unsigned long dwMsgMapID) Line 168  C++
    Test.exe!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::WindowProc(HWND__ * hWnd, unsigned int uMsg, unsigned __int64 wParam, __int64 lParam) Line 3523    C++
and does set `VLD_HEAP_CRT_DBG` because __crtalloc=true__

There is a known fix for this ?
Oct 22, 2013 at 8:28 AM
I have a fix for this. Who can help with guidance ?