Easy memory Detection

Source: Internet
Author: User

When I first came into contact with C ++, I always felt that memory leakage was a terrible thing, and I felt helpless until one day I found that the boulder was such a small case.
Method 1: Use CRT to debug heap Functions
Example:
# Ifdef _ DEBUG
# Define CRTDBG_MAP_ALLOC
# Include <crtdbg. h>
# Include <stdlib. h>
# Define new (_ NORMAL_BLOCK ,__ FILE __,__ LINE __)
# Endif

Int main ()
{
Char * p = new char [200];
# Ifdef _ DEBUG
_ CrtDumpMemoryLeaks ();
_ CrtSetDbgFlag (_ CRTDBG_ALLOC_MEM_DF | _ CRTDBG_REPORT_FLAG | _ CRTDBG_LEAK_CHECK_DF );
# Endif

Return 0;
}

Output Information:
Detected memory leaks!
Dumping objects->
F: \ test \ highquality \ c ++ primer \ main. cpp (10): {69} normal block at 0x00396990,200 bytes long.
Data: <> CD
Object dump complete.

Analysis:
{Xxx}-indicates that the memory allocation leaked for the xxx time after the program was run, that is, the memory allocation number;
Xxx block-indicates the memory block type, including normal, client, and CRT );
At 0x00396990-indicates the memory address leaked, expressed in hexadecimal format;
Xx bytes long-indicates the memory size leaked;
Data: xxx-indicates the memory Data information, which is usually the first 16 bytes.

When _ CrtSetBreakAlloc () is used to allocate the specified memory, it is interrupted and the call stack is viewed.
# Ifdef _ DEBUG
# Define CRTDBG_MAP_ALLOC
# Include <crtdbg. h>
# Include <stdlib. h>
# Define new (_ NORMAL_BLOCK ,__ FILE __,__ LINE __)
# Endif

Int main ()
{
_ CrtSetBreakAlloc (69 );
Char * p = new char [200];
# Ifdef _ DEBUG
_ CrtDumpMemoryLeaks ();
_ CrtSetDbgFlag (_ CRTDBG_ALLOC_MEM_DF | _ CRTDBG_REPORT_FLAG | _ CRTDBG_LEAK_CHECK_DF );
# Endif

Return 0;
}

Method 2: Use VLD
Visual Leak Detector is a free memory Leak detection tool for Visual C ++. Compared with other memory leak detection tools, it detects memory leaks and has the following features:
(1). You can obtain the call stack of the memory leak point. If you can, you can also obtain the file and row number of the memory leak point;
(2). You can obtain complete data that leaks the memory;
(3) You can set the Memory Leak report level;
(4). It is a packaged lib and does not need to compile its source code during use. You only need to make minor changes to your own code;
(5). Its source code is released with the GNU license, with detailed documents and comments. It is a good choice for readers who want to learn more about heap memory management.
1. Download VLD tool http://www.codeproject.com/tools/visualleakdetector.asp
2. decompress the package to obtain files such as vld. h, vldapi. h, vld. lib, vldmt. lib, vldmtdll. lib, and dbghelp. dll. Set. h file and. copy the lib file to the directory where the project file you want to detect is located (only for this project), and copy dbghelp. dll copy to the running directory of your program.
3. Usage: add the statement # include "vld. h" to the. cpp file that contains the entry function.
After compilation is correct, run the following command in debug mode: View VC output information:
Memory leakage: "WARNING: Visual Leak Detector detected memory leaks! "
No memory leakage. The output information is: "No memory leaks detected ."
4. Examples:
# Include <iostream>
# Include <string>
# Include "vld. h"

Using namespace std;

Int main ()
{
Int * n = new int [10];
String * s = new string ("hello! \ N ");

Cout <n;
Cout <* s;

Return 0;
}

The output information contains the following statements:
Visual Leak Detector Version 1.0 installed (multithreaded static ).
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 84 at 0x003E6CB8: 32 bytes ----------
Call Stack:
F: \ test \ highquality \ c ++ primer \ main. cpp (10): main
F: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ crt0.c (327): _ tmainCRTStartup
F: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ crt0.c (196): mainCRTStartup
0x7C817077 (File and line number not available): RegisterWaitForInputIdle
Data:
00 00 00 00 CD 68 65 6C 6C 6F 21 0A 00 ...... hello !..
CD 07 00 00 00 0F 00 00 00 ................

---------- Block 83 at 0x003E6C50: 40 bytes ----------
Call Stack:
F: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ newaop. cpp (7): operator new []
F: \ test \ highquality \ c ++ primer \ main. cpp (9): main
F: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ crt0.c (327): _ tmainCRTStartup
F: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ crt0.c (196): mainCRTStartup
0x7C817077 (File and line number not available): RegisterWaitForInputIdle
Data:
CD ................
CD ................
CD ................

Visual Leak Detector detected 2 memory leaks.
Export c00000000primer.exe ":" C: \ WINDOWS \ system32 \ dbghelp. dll "has been uninstalled"
Export c00000000primer.exe ":" C: \ WINDOWS \ system32 \ version. dll "has been uninstalled"
Visual Leak Detector is now exiting.
The program "[1740] c00000000primer.exe: Local Machine" has exited and the returned value is 0 (0x0 ).

The code is changed:
# Include <iostream>
# Include <string>
# Include "vld. h"

Using namespace std;

Int main ()
{
Int * n = new int [10];
String * s = new string ("hello! \ N ");

Cout <n;
Cout <* s;

Delete [] n;
Delete s;

Return 0;
}

The output information contains the following statements:
Visual Leak Detector Version 1.0 installed (multithreaded static ).
No memory leaks detected.
Export c00000000primer.exe ":" C: \ WINDOWS \ system32 \ dbghelp. dll "has been uninstalled"
Export c00000000primer.exe ":" C: \ WINDOWS \ system32 \ version. dll "has been uninstalled"
Visual Leak Detector is now exiting.
The program "[4272] c00000000primer.exe: Local Machine" has exited and the returned value is 0 (0x0 ).

 

Author: one_in_one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.