Steps to detect memory leak (draft)

Source: Internet
Author: User
Tags exit in

1. Use _ crtdumpmemoryleaks () to check whether there is memory leak in program.

 

With the help of _ crtdbg_map_alloc, it can output Memory Leak info with file line info for those memory block allocated by malloc (), while it wowould never print file line info for those allocated by new ().

 

#define _CRTDBG_MAP_ALLOC#include <stdlib.h>
#include <crtdbg.h>

 

Call

{

_ Crtdumpmemoryleaks ();

}

At the end of the program exit if there is only one exit in the program.

 

Or

Call

{

_ Crtsetdbgflag (_ crtdbg_alloc_mem_df | _ crtdbg_leak_check_df );

}

At the beginning of your program to ensure _ crtdumpmemoryleaks () wocould be called when the program exits from any exit point.

 

2. Use debug_new method to check in detail

 

# Define debug_new new (_ normal_block, this_file, _ line __)

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;

# Endif

 

{

_ Crtmemdumpallobjectssince (null );

}

 

3. Use tool like leakfinder (here: http://www.codeproject.com/KB/applications/leakfinder.aspx)

I still prefer to use shared_ptr to manage heap allocation instead of putting too much effort on detecting memory leak.

 

Keep on learning.

 

 

 

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.