主要方法:利用系統帶的函數:EnableMemLeakCheck() 和函數重載,能快速準備的定位到內存泄漏的地方,方法簡單且實用,值得借用。
?
#include <crtdbg.h>
#ifdef_DEBUG?
#define new?
#endif
void EnableMemLeakCheck()?
{
?
}
void main()
{
?
?
}
運行后得到的消息:
Loaded 'C:\WINNT\System32\ntdll.dll', no matchingsymbolic information found.
Loaded 'C:\WINNT\system32\kernel32.dll', no matching symbolicinformation found.
Detected memory leaks!
Dumping objects ->
D:\DS_Learn\meleak\test1.cpp(19) : {39}normal block at 0x00430120, 40 bytes long.
?
Object dump complete.
The thread 0x91C has exited with code 4391200 (0x430120).
The program 'D:\DS_LEARN\meleak\Debug\meleak.exe' has exited withcode 4391200 (0x430120).
很快就能定位到內存泄漏的地方。
我原來也利用相關的方法,在C代碼中,重載malloc 和free,也能很好的檢測有沒有內存泄漏和發生泄漏的地方,方法都很簡單,其實自己在寫代碼注意一下結構,還是很容易避免發生內存泄漏。
?
?
?
?