先祝各位朋友新年快乐,:)使用VS2008 写的MFC程序,使用Visual Leak Detector检测内存泄露 的疑问,请教各位前辈,谢谢帮忙:)有个怪事:选择“在静态库中使用 MFC”就会出现内存泄露
(Visual Leak Detector detected 47 memory leaks in heap 0x04340000),而选择“在共享 DLL 中使用 MFC”就不会内存泄露。

以下是内存泄露的输出日志,请帮忙分析,是什么原因导致内存泄露的,由于日志太长,只粘出一部分,其他的都差不多,没有明确定位内存泄露。
-------------------------------------------------------------------
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x04341EA0: 568 bytes ----------
  Call Stack:
    f:\dd\vctools\crt_bld\self_x86\crt\src\tidtable.c (395): RUNH32.dll!_mtinit + 0x18 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\dllcrt0.c (109): RUNH32.dll!_CRT_INIT + 0x5 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\dllcrt0.c (312): RUNH32.dll!__DllMainCRTStartup + 0x11 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\dllcrt0.c (281): RUNH32.dll!_DllMainCRTStartup + 0x11 bytes
    0x7C92118A (File and line number not available): ntdll.dll!LdrInitializeThunk + 0x24 bytes
    0x7C93C4DA (File and line number not available): ntdll.dll!LdrFindResourceDirectory_U + 0x28D bytes
    0x7C936351 (File and line number not available): ntdll.dll!RtlValidateUnicodeString + 0x507 bytes
    0x7C9364B3 (File and line number not available): ntdll.dll!LdrLoadDll + 0x110 bytes
    0x7C801BBD (File and line number not available): KERNEL32.dll!LoadLibraryExW + 0xC8 bytes
    0x79EEBD50 (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x3FE5 bytes
    0x79EEBDA8 (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x403D bytes
    0x79EEBE3C (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x40D1 bytes
    0x79EEF188 (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x741D bytes
    0x79EEF343 (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x75D8 bytes
    0x79EA358E (File and line number not available): mscorwks.dll!CoUninitializeEE + 0xE422 bytes
    0x79EA3283 (File and line number not available): mscorwks.dll!CoUninitializeEE + 0xE117 bytes
    0x79EA31BF (File and line number not available): mscorwks.dll!CoUninitializeEE + 0xE053 bytes
    0x79EA2640 (File and line number not available): mscorwks.dll!CoUninitializeEE + 0xD4D4 bytes
    0x79E80827 (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x00431F3E (File and line number not available): (Module name unavailable)!(Function name unavailable)
    0x00D971E2 (File and line number not available): (Module name unavailable)!(Function name unavailable)
    0x00D96DA2 (File and line number not available): (Module name unavailable)!(Function name unavailable)
    0x79E71B4C (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x79E8908A (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x7A16D023 (File and line number not available): mscorwks.dll!TranslateSecurityAttributes + 0x34F30 bytes
    0x7A16D4BA (File and line number not available): mscorwks.dll!TranslateSecurityAttributes + 0x353C7 bytes
    0x79279583 (File and line number not available): mscorlib.ni.dll!(Function name unavailable)
    0x79875A92 (File and line number not available): mscorlib.ni.dll!(Function name unavailable)
    0x79875A54 (File and line number not available): mscorlib.ni.dll!(Function name unavailable)
    0x792CA603 (File and line number not available): mscorlib.ni.dll!(Function name unavailable)
    0x792CA499 (File and line number not available): mscorlib.ni.dll!(Function name unavailable)
    0x79E71B4C (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x79E8908A (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x79E8988B (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x79E898CC (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x79E89939 (File and line number not available): mscorwks.dll!(Function name unavailable)
    0x79FBA7DD (File and line number not available): mscorwks.dll!NGenCreateNGenWorker + 0xA0F4 bytes
    0x79EC1457 (File and line number not available): mscorwks.dll!InstallCustomModule + 0x1DB8F bytes
    0x79EC13F3 (File and line number not available): mscorwks.dll!InstallCustomModule + 0x1DB2B bytes
    0x79EC1313 (File and line number not available): mscorwks.dll!InstallCustomModule + 0x1DA4B bytes
    0x79EC14A5 (File and line number not available): mscorwks.dll!InstallCustomModule + 0x1DBDD bytes
    0x79EC22C0 (File and line number not available): mscorwks.dll!InstallCustomModule + 0x1E9F8 bytes
    0x79FBA472 (File and line number not available): mscorwks.dll!NGenCreateNGenWorker + 0x9D89 bytes
    0x79EC1F5A (File and line number not available): mscorwks.dll!InstallCustomModule + 0x1E692 bytes
    0x79FCB469 (File and line number not available): mscorwks.dll!CorExitProcess + 0x503B bytes
    0x7C80B729 (File and line number not available): KERNEL32.dll!GetModuleFileNameA + 0x1BA bytes....(略)---------- Block 55 at 0x04342208: 76 bytes ----------
  Call Stack:
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp (141): RUNH32.dll!CAfxStringMgr::Allocate + 0x9 bytes
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (775): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::Fork + 0x3C bytes
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (812): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::PrepareWrite2 + 0xC bytes
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (801): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::PrepareWrite
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (524): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::GetBuffer
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (639): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::SetString + 0xC bytes
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (616): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::SetString
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlsimpstr.h (332): RUNH32.dll!ATL::CSimpleStringT<wchar_t,0>::operator=
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\cstringt.h (1033): RUNH32.dll!ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::operator=
    e:\program files\microsoft visual studio 9.0\vc\atlmfc\include\cstringt.h (810): RUNH32.dll!ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >
    f:\dd\vctools\vc7libs\ship\atlmfc\include\afxcomctl32.h (506): RUNH32.dll!CShellWrapper::CShellWrapper + 0x37 bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxstate.cpp (190): RUNH32.dll!AFX_MODULE_STATE::AFX_MODULE_STATE + 0x1C bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxstate.cpp (429): RUNH32.dll!_AFX_BASE_MODULE_STATE::_AFX_BASE_MODULE_STATE + 0x13 bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\include\afxtls_.h (227): RUNH32.dll!CProcessLocal<_AFX_BASE_MODULE_STATE>::CreateObject + 0x49 bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxtls.cpp (464): RUNH32.dll!CProcessLocalObject::GetData + 0x3 bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\include\afxtls_.h (213): RUNH32.dll!CProcessLocal<_AFX_BASE_MODULE_STATE>::GetData + 0xD bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxstate.cpp (467): RUNH32.dll!AfxGetModuleState + 0xA bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\cmdtarg.cpp (24): RUNH32.dll!CCmdTarget::CCmdTarget + 0x5 bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\thrdcore.cpp (475): RUNH32.dll!CWinThread::CWinThread + 0x30 bytes
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\appcore.cpp (369): RUNH32.dll!CWinApp::CWinApp + 0x32 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (903): RUNH32.dll!_initterm
    f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (307): RUNH32.dll!_cinit + 0xF bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\dllcrt0.c (132): RUNH32.dll!_CRT_INIT + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\dllcrt0.c (312): RUNH32.dll!__DllMainCRTStartup + 0x11 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\dllcrt0.c (281): RUNH32.dll!_DllMainCRTStartup + 0x11 bytes
    0x7C92118A (File and line number not available): ntdll.dll!LdrInitializeThunk + 0x24 bytes
    0x7C93C4DA (File and line number not available): ntdll.dll!LdrFindResourceDirectory_U + 0x28D bytes
    0x7C936351 (File and line number not available): ntdll.dll!RtlValidateUnicodeString + 0x507 bytes
    0x7C9364B3 (File and line number not available): ntdll.dll!LdrLoadDll + 0x110 bytes
    0x7C801BBD (File and line number not available): KERNEL32.dll!LoadLibraryExW + 0xC8 bytes
    0x79EEBD50 (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x3FE5 bytes
    0x79EEBDA8 (File and line number not available): mscorwks.dll!GetMetaDataInternalInterface + 0x403D bytes....(略)Visual Leak Detector detected 47 memory leaks in heap 0x04340000
No memory leaks detected.
Visual Leak Detector is now exiting.
VC内存泄露MFCVisual Leak Detector

解决方案 »

  1.   

    还有,我的机器上并没有f:\dd ... 这些文件
      

  2.   


    仔细检查过,没有呀,并且选择“在共享 DLL 中使用 MFC”就不会内存泄露。
      

  3.   

    内存泄露有的时候是虚警
    尤其是加载dll和lib的相关操作
    所以你这样确定自己的程序没有问题就好
    也可以用windows的性能计数器来查看
      

  4.   

    为什么
    选择“在静态库中使用 MFC”会出现内存泄露
    而选择“在共享 DLL 中使用 MFC”就不会内存泄露?,这是为什么呀?
      

  5.   


    “windows的性能计数器”是什么程序呀?木有用过,帮忙进一步指点下,谢谢
      

  6.   

    不客气
    百度一下就有了
    http://baike.baidu.com/view/8029329.htm
      

  7.   

    Visual Leak Detector detected 47 memory leaks in heap 0x04340000
    No memory leaks detected.
    Visual Leak Detector is now exiting.???
      

  8.   


    1. Visual Leak Detector detected 47 memory leaks in heap 0x04340000
    2. No memory leaks detected.1和2是不是矛盾呀?
      

  9.   

    ATL::CSimpleStringT<wchar_t,0>::GetBufferCString::GetBuffer();  你的程序中有用这个函数吗?
    这个函数好像会在堆中分配空间,之后需要调用CString::ReleaseBuffer();函数来释放
      

  10.   

    是不是在线程中调用 了MFC,线程在进程退出后没有退出就可能有这个问题
      

  11.   


    有用过CString::GetBuffer();,是作为函数的参数,这该怎么释放呀?
      

  12.   

    不要用这个函数,CString提供了一个方法:
    CString str = "你好";
    LPCTSTR ps = str.operator LPCTSTR();
      

  13.   

    请教,如果是LPWSTR 应该如何做呀?
      

  14.   

    你是说,参数是LPWSTR吗?你需要修改这个参数吗?
    如果不需要,你把参数改成LPCWSTR,这样就可以用CString.operator LPCWSTR();了
      

  15.   


    是在线程中调用了MFC,请问这该如何解决呀?谢谢你