Memory leakage detection in Windows

Source: Internet
Author: User
It is relatively easy to develop and detect memory leaks in windows. Here is just a simple record of some things, without specific implementation. In Windows, the memory detection mainly involves two aspects. These two aspects are not an if else branch.
1. If you are in the development environment of MS (other development environments are not very familiar with it), you can use the debugging library defined in MS, which is relatively simple to use, below is a simple example:
# Include <crtdbg. h>
Int main () {char * pstring = new char [8]; _ crtdumpmemoryleaks (); Return 0 ;}
Through the above example, we will know how to use these debugging libraries. The first is to protect the header files of this debugging library. The specific debugging Library provides the APIs that can be accessed. see in the H file. Dump meory lead is only one of them. Note that these functions can only be used in the debug version. Program . Another practical example is to compare Memory changes by setting the memory status detection point to locate memory leaks. The three API functions involved are: Set the memory status detection point: _ crtmemcheckpoint (& memstate); compare the differences between memory status detection points: _ crtmemdifference (& memstate_difference, & memstate_original, & memstate_now); differences in Display memory status: _ crtdumpallobjectssince (& memstate_defference); For details, refer to msdn.

2. Another method is to reload the memory allocation and release functions such as new, delete, malloc, and free, and set a list to record the memory reinsurance situation. You can use this framework to implement this public: A, # define new debug_new
B. # define debug_new (nsize) newfunc (nsize), this_file, _ line _) // or implement it by reloading the new method.
C, void * newfunc (size_t size, const char * pfilename, int nline)
{
// Lock list
// Allocate memory
// Add the memroy object to the list
// Unlock list
Return memory;
}
D. The implementation of free is similar.
In this way, when the program is launched, you can dump out memory allocated but not released. Most self-implemented methods to prevent memory leakage are the same.

MS in MFC also provides a class cmemorystate and Some APIs, afxcheckmemory, afxdumpmemoryleaks... are all very good methods.

the recommended method for detecting Memory leakage in MS is as follows:
1. The debug_new method is used to replace the new method to realize memory reinsurance, in many MFC projects. in the CPP file, we can see a lot of define new debug_new for this purpose.
2. allows memory diagnosis. You can use the afxenablememorytracking function to enable memory diagnosis. If you do not use the MFC project, you can use the _ crtsetdbgflag function to set the _ crtdbg_alloc_mem_df identifier. For details, refer to the source code of this function. After setting, you can use the API in afxcheckmemory to perform memory detection. In fact, this function also directly calls the _ crtcheckmemory function, so that the _ crtcheckmemory function can be directly called in a non-MFC environment.
3. take a snapshot of the memory to identify the memory leakage by comparing the changes in the memory snapshot. This is the method mentioned above.
4. Check the memory statistics. You can use cmemorystate: dumpstatistics to check the usage of the cmemorystate class.
5. Object dump. The dump of an object is completed through cmeorystate: dumpallobjectssince
.

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.