How to locate c ++ Memory leakage

Source: Internet
Author: User

During C ++ development, if you use MFC on windows, you can automatically detect memory leakage. If you use win32 or console programs, you do not use MFC, you need to process it yourself. The following are some of the methods summarized here, all from the Internet and have undergone practical tests. 1. KDetectMemoryLeak. h is used to redefine the NEW macro similar to MFC. You can complete the output of the leak point. At the end of the Code, use _ CrtDumpMemoryLeaks (). For details, see the VS2008 Memory leakage detection document. The author wrote it clearly. Thank you. KDetectMemoryLeak. h source code: # pragma once # ifdef _ DEBUG # define DEBUG_CLIENTBLOCK new (_ CLIENT_BLOCK, _ FILE __,__ LINE __) # else # define DEBUG_CLIENTBLOCK # endif # define _ CRTDBG_MAP_ALLOC # include <stdlib. h> # include <crtdbg. h> # ifdef _ DEBUG # define new DEBUG_CLIENTBLOCK # endif 2. for leakage of specific source code that cannot be located, you can locate it by analyzing the information dumped by dump. This is often the case especially in DLL libraries. For example: {152} normal block at 0x01078320, 12 bytes long. data: 08 79 65 00 48 82 07 01 00 00 00 00 {151} normal block at 0x010782C0, 36 bytes long. data: CD 00 00 00 00 00 00 00 0000 {150} normal block at 0x01078248, 56 bytes long. data: F4 77 65 00 00 00 00 00 68 71 07 01 90 3A 0910 page 702 of "software debugging" describes the details of the heap block dump. Take one of the heap blocks as an example: {137} normal block at 0x01073790, 8 bytes long. data: <H7> 48 37 07 01 CD 137 is the allocation serial number of the heap block. The heap block type is normal heap block. Its position is 0x01073790, and the user zone length is 8 bytes. Data is the first 16 bytes of the user Data zone. Because the Data zone is only 8 bytes, it is all 8 bytes. <H7> the 8-byte ASCII code Display (the other 6 bytes are non-printable ASCII codes ). The cd cdcd is the fixed content automatically filled by the CRT when the heap block is allocated ("software debugging" P696 ). This indicates that the application has used the first four bytes of heap blocks of these eight bytes, and the last four bytes have not been used. There are many methods for such Memory leakage. First, you can try the memory allocation sequence breakpoint described in section 23.13.1, that is, re-run the program, set the sequence breakpoint (set the _ crtBreakAlloc variable to the sequence number to be interrupted, such as 137) as early as possible (such as the entry), so that the CRT is interrupted when allocated to the specified memory block. After interruption, you can determine which module is allocating Memory Based on Stack backtracking, record it, and exit the program. If the heap block is still in the dump list, then the module is doubtful. If you have suspected Module source code, you can use the method described in section 23.15.3 to make the heap block dump information contain the source program file name and line number, that is, the following: C: \ dig \ dbg \ author \ code \ chap23 \ MemLeak. cpp (22): {74} normal blocks at 0x00371000, 20 bytes long. data: CD 3. you can use vld for detection. 4. You can also use tools such as boundcheck and purify for detection.

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.