Visual 2010 界面库的内存泄漏问题
我使用Visual 2010中的最新的界面库(仿Visual Studio风格)编写程序,打开程序,直接关闭程序,Visual 2010就检测到有内存泄漏,提示如下:
Detected memory leaks!
Dumping objects ->
。。。。。。。。。。。。。
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {132} normal block at 0x01346FC8, 19 bytes long.
Data: <T y > 54 CD 07 79 02 00 00 00 02 00 00 00 01 00 00 00
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {131} normal block at 0x01346F70, 26 bytes long.
Data: <T y > 54 CD 07 79 09 00 00 00 09 00 00 00 01 00 00 00
Object dump complete.
通过检测dbgheap.c中的new的过程,在“
/* break into debugger at specific memory allocation */
if (_crtBreakAlloc != -1L && lRequest == _crtBreakAlloc)
_CrtDbgBreak();
设置断点,发现这些内存泄漏的地方全部是和界面上的pane、toolbar等相关的静态变量,如{131} block的调用栈是这样的:
> msvcr100d.dll!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) 行 392
C++
msvcr100d.dll!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) 行 239 + 0x19 字节
C++
msvcr100d.dll!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) 行 302 + 0x1d 字节
C++
msvcr100d.dll!_malloc_dbg(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine) 行 160 + 0x1b 字节
C++
mfc100d.dll!CAfxStringMgr::Allocate(int nChars, int nCharSize) 行 156 + 0x16 字节
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::Fork(int nLength) 行 809 + 0x26 字节
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::PrepareWrite2(int nLength) 行 849 + 0xc 字节
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::PrepareWrite(int nLength) 行 838
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::GetBuffer(int nMinBufferLength) 行 535
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::SetString(const char * pszSrc, int nLength) 行 654 + 0xc 字节
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::SetString(const char * pszSrc) 行 629
C++
mfc100d.dll!ATL::CSimpleStringT<char,1>::operator=(const char * pszSrc) 行 342
C++
mfc100d.dll!ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::operator=(const char * pszSrc) 行 1235
C++
mfc100d.dll!ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >(const char * pszSrc) 行 968
C++
mfc100d.dll!`dynamic initializer for 'strBaseControlBarProfile''() 行 44 + 0x14 字节
C++
msvcr100d.dll!_initterm(void (void)* * pfbegin, void (void)* * pfend) 行 873
C
mfc100d.dll!_CRT_INIT(void * hDllHandle, unsigned long dwReason, void * lpreserved) 行 284 + 0xf 字节
C
mfc100d.dll!__DllMainCRTStartup(void * hDllHandle, unsigned long dwReason, void * lpreserved) 行 506 + 0x11 字节
C
mfc100d.dll!_DllMainCRTStartup(void * hDllHandle, unsigned long dwReason, void * lpreserved) 行 476 + 0x11 字节
C
双击“mfc100d.dll!`dynamic initializer for 'strBaseControlBarProfile''() 行 44 + 0x14 字节
C++“
定位到afxbasepane.cpp中如下行:
static const CString strBaseControlBarProfile = _T("BasePanes");
类似的,其他的内存泄漏的地方都是这些静态变量的地方。感觉很困惑,这些都是静态变量,而且又不是new出来的,为什么有内存泄漏呢?
试了下VC的sample,就没有这种内存泄漏提示。
谢谢大家。