Test procedure: Http://download.csdn.net/detail/ajaxhe/4085447vld-2.2.3:http://vld.codeplex.com/releases/view/82311 method One:
Reference link 1:http://blog.csdn.net/dizuo/article/details/6030676
Reference link 2:http://www.schmidt-web-berlin.de/winfig/blog/?p=154
Reference link 1And
Reference Link 2Say is a thing, do not use any tool, write your own program to determine the overflow location. Method Two: Reference link 3:http://blog.csdn.net/dyw/article/details/6860473 reference link 4:http://hi.baidu.com/sunchongjing/blog/item/ Cd1920faf61ee7d3b48f3108.html
Reference Link 3And
Reference Link 4is to use
VLD (Visual Leak Detector) to detect memory overflow, of course, more professional.
Problem Description:
A memory leak detection statement was simply added today in QT_VP:
_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);
The results come out a lot of memory leaks, really scared a jump, tracking a day, piecemeal sent, finally still feel no problem (or more confident Code quality O (∩_∩) o haha ~) ... Finally on the Internet, in VS, the memory leak report, then uninstall the QT DLL, release the requested resources. That's why this is happening. |
System environment: Windows7 + VS2008; Method One: The method of operation is consistent with the reference link 1 approach, as follows: Flag Memory Detection Report: SETDEBUGNEW.H (note that memory leak tracking is only possible if you add setdebugnew.h to the CPP and those functions or classes with the new operation are called)
#ifndef set_debug_new_h#define set_debug_new_h #ifdef _debug#define debug_clientblock NEW(_client_block, __ file__, __line__) #else # define DEBUG_CLIENTBLOCK#ENDIF #define _crtdbg_map_alloc#include <crtdbg.h> #ifdef _ Debug#define New debug_clientblock#endif #endif//End Set_debug_new_h |
Include the setdebugnew.h header file in the CPP file that you want to detect Test_memoryleak.cpp
#include "test_memoryleak.h" #include <QtGui> #include "setdebugnew.h" Test_memoryleak::test_memoryleak ( Qwidget *parent, Qt::wflags flags): Qwidget (parent, flags) {BTN1 =NewQpushbutton ("Test1", This); BTN2 =NewQpushbutton ("Close", This); Qwidget *memleak_test =NewQwidget; Connect (BTN2, SIGNAL (clicked ()), This, SLOT (Close ())); Qhboxlayout *layout =NewQhboxlayout; Layout->addwidget (BTN1); Layout->addwidget (BTN2); SetLayout (layout);} Test_memoryleak::~test_memoryleak () {} |
main.cpp (set _crtsetdbgflag at the entrance of the main function (&NBSP;_CRTDBG_ALLOC_MEM_DF&NBSP;|&NBSP;_CRTDBG_LEAK_CHECK_DF );)
#include "test_memoryleak.h" #include <QtGui/QApplication> #include "setdebugnew.h" int main (int argc, char *argv[]) {#ifdef _DEBUG _CrtSetDbgFlag (_crtdbg_alloc_mem_df | _crtdbg_leak_check_df); #en dif int *i = new int; Qapplication A (argc, argv); Test_memoryleak W; W.show (); return a.exec ();} |
Start debugging, and view the contents of the output item after the program has finished running
{1220} Normal block at 0x016605f8, 552 bytes Long. data: < F > The "$" CD CD cd{1215} normal block at 0x01660398, 292 bytes Long. data: < EH f x=g> 94-A2-C4 3D . \test_memoryleak.cpp: {1214} client block at 0x01660348, subtype 0, bytes long. ... {289} normal block at 0x01388960, bytes long. data: < 8 > at 8A, cd CD CD, XX, XX, . \main.cpp: {285} client B Lock at 0x013872b0, subtype 0, 4 bytes long. data: < > CD cd CD cdobject dump complete. |
The bold portion of bold is where the program really overflows.
Method Two:Use VLD to detect memory overflow, vld:http://vld.codeplex.com/releases/view/82311 (2.2.3 version only support vs2008/2010) installation process will pop up the following dialog box, the new version solves the path problem, So we don't have to do too much setup. After the installation is complete, you will also need to set the compilation path for VS, add the "Include file" path and the "library file" path to the "VC + + Directory", "Tools", "Options", "Projects and Solutions", as shown below: VLD.
Note: Refer to link 4mentioned, in the Win7 need to copy Dbghelp.dll and Vdl_x86.dll to system32 directory, and I do not need to do this when configuring. Include the vld.h header file in the Main.cpp, as follows:
#include "test_memoryleak.h" #include <QtGui/QApplication> #ifdef _debug #include "vld.h" #endif #include "setdebugnew.h" int main (int argc, char *argv[]) {#ifdef _DEBUG _crt Setdbgflag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df); #endif int *i = new int; Qapplication A (argc, argv); Test_memoryleak W; W.show (); return a.exec ();} |
Start debugging, and view the contents of the output item after the program has finished running
Warning:visual Leak Detector detected memory leaks!----------Block 1 at 0x007f72b0:4 bytes---------- Call Stack: C:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\main.cpp (17): Test_memoryleak.exe!main + 0x10 bytes C:\Qt\4.7.4\src\winmain\qtmain_win.cpp (131): Test_ memoryleak.exe! WinMain + 0x12 bytes f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): test_memoryleak.exe!__ Tmaincrtstartup + 0x35 bytes f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): Test_ memoryleak.exe! winmaincrtstartup 0x76b9ed6c (File and line number not available): kernel32.dll! Basethreadinitthunk + 0x12 bytes 0x77d1377b (File and line number not available): ntdll.dll! Rtlinitializeexceptionchain + 0xEF bytes 0x77d1374e (File and line number not available): ntdll.dll! Rtlinitializeexceptionchain + 0xC2 bytes data: CD CD CD ... ........ ---- ------Block 4 at 0x00ca0348:20 bytes---------- Call stack: C:\USERS\AJAXHE\DESKTOP\PROGRAM\QT \practice\test_memoryleak\test_memoryleak\test_memoryleak.cpp (): Test_memoryleak.exe!test_memoryleak::test_ Memoryleak + 0x10 bytes c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_ Memoryleak\main.cpp: Test_memoryleak.exe!main + 0x17 bytes C:\Qt\4.7.4\src\winmain\qtmain_ Win.cpp (131): test_memoryleak.exe! WinMain + 0x12 bytes f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): test_memoryleak.exe!__ Tmaincrtstartup + 0x35 bytes f:\dd\vctools\crt_bld\seLF_X86\CRT\SRC\CRTEXE.C (403): test_memoryleak.exe! winmaincrtstartup 0x76b9ed6c (File and line number not available): kernel32.dll! Basethreadinitthunk + 0x12 bytes 0x77d1377b (File and line number not available): ntdll.dll! Rtlinitializeexceptionchain + 0xEF bytes 0x77d1374e (File and line number not available): ntdll.dll! Rtlinitializeexceptionchain + 0xC2 bytes data: 7C FB 00 98 CA 00 &nbs p; E0 7B FB 00 xx CD cd . | ...... . {...... CA 00 P .... visual Leak Detector detected 2 memory leaks (bytes, all-in). Largest number used:260 bytes. Total allocations:260 bytes. Visual Leak Detector is now exiting. |
Overflow position consistent with expectations!
http://blog.csdn.net/itjobtxq/article/details/21785885
QT vs Detect memory leaks