Memory leak checking method for C + + development on Windows platform

Source: Internet
Author: User

Memory leak checking method for C + + development on Windows platform full use of debugging tools makes it easy to avoid memory leaks. Here are two ways to complement each other, the first is the VC compiler to provide the method, the second is a dedicated memory leak detection tool memmory Validator. The basic principle of the two methods is the same: the memory allocation to be implemented through the CRT at runtime, as long as the memory allocated and free memory when the record, the program at the end of the comparison between allocating memory and releasing memory records can determine whether there is a memory leak. The first method overloads the new operator, and the second replaces the CRT Run-time Library, adding a layer between the user program and the runtime to record memory allocations. The difference between the two approaches is that the former is done at compile time, the code that analyzes the memory is compiled into the execution file, used for the debug version of the program, the latter has no effect on the compilation process, and the interaction information of the CRT is intercepted during the execution process. The first method is described in MSDN, in the need to check the memory allocation of CPP file reference and two headers, put in the head, and then use the method of macro substitution with the new operator after overloading replace the original new operator, as shown in the following code, in the location of the program exit call _ Crtdumpmemoryleaks () Output all the memory content that has not been released, and request the source code location of these memory, very easy to debug. However, this approach is flawed and _crtdumpmemoryleaks () must be invoked before the end of the main () function, so the stack object in the main () function has not yet been refactored and will be used as a memory leak, as is the case with the Newclass class in the following code, which is actually no problem. The other is that different compilers may differ when implementing non-ASCII characters, such as the Chinese character string in the following code is considered to be a memory leak. #include #include #include #include #include using namespace std; #define DEBUG_NEW New (_normal_block, __file__, __line__) #define NEW debug_new struct Alertdescriptionstring {unsigned in T ID; String Name; String Desp; }; Const alertdescriptionstring string[] = {0, "AAA", "BBBB"},//Memory leak count by _crtdbg_map_alloc {1, "CCC", "__xxx__ detected Possibly by aIrjack sent Packets "}}; struct ALERTDESCRIPTIONSTR {unsigned int ID; char* Name; char* desp;}; Const ALERTDESCRIPTIONSTR str[] = {0, "AAA", "bbbb"}, {1, "CCC", "detected packets that may be emitted by Airjack"}}; void Leak_memory (void) {vector vec; for (int j = 0; J < 2; J +) {Vec.push_back (new unsigned int (0));} class Newcla ss {void * p; Public:newclass () {p = (void*) new char[10];} ~newclass () {delete [] p;}}; int * p = new int (9); int real_main_fun (int argc, char * * argv) {leak_memory (); return 0;} int main (int argc, char * * argv) {string str ("__ yyy__ detection of packets that may be sent by Airjack "); Newclass CLS; int i = Real_main_fun (argc, argv); The original main function body if (_CrtDumpMemoryLeaks ()) cout<< "Memory leak" << Endl; return i; The second method is exactly what it complements, Memmory Validator can check the memory allocation during the entire run of the program, or display the location of the memory leak, as shown in the figure. In the actual application of the first method, you can use two header files, for large-scale engineering debugging, almost no other part of the code has an impact. -----------------------------------------------------------------------------//config.h #define MEMORY_DEBUG # ifdef memory_debug #include #include #endif//MemorY_debug------------------------------------------------------------------------------//debug.h #ifdef memory_ DEBUG #define DEBUG_NEW New (_normal_block, __file__, __line__) #define NEW debug_new #endif//Memory_debug-------------- -----------------------------------------------------------------//main.cpp #include "config.h"//config.h, first header file #include "alertparser.h" #include "alertinfocache.h" #include #include #include "debug.h"//debug.h, the last header file int main (int ARGC, char * * argv) {int i = Real_main_fun (argc, argv);//original main function body if (_CrtDumpMemoryLeaks ()) cout<< "Memory le AK "<< Endl; return i; }

Related Article

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.