VisualStudio How to use visual Leak Detector

Source: Internet
Author: User
Tags stack trace

So what's a good memory leak detection tool under Windows? Microsoft provides Visual Studio development tools itself without much good memory leak detection capabilities, and we can use third-party tools for visual Leak Detector (hereinafter referred to as VLD).

VLD tool is a small and easy-to-use, free open source memory leak detection Tool in VC + + environment, VLD can display the full memory allocation call stack which causes memory leak. The VLD Detection report provides a complete stack trace for each memory leak point and contains its source and line number information.

The installation process is to download the VLD installation file to address http://vld.codeplex.com/, then install it, and install the program to configure environment variables during installation. We need to remember the installation directory.

After the installation is complete, open the Visual Studio project that you want to detect, and we need to configure it in the project: VLD header file directory and VLD library directory.

Select the game Project, open the menu "project" → "properties" pop-up project Properties dialog box, select "Configuration Properties" → "VC + + Directory" → "General", add C:\Program Files (x86) \visual Leak detector\ in "Include directory" on the right Include, where C:\Program Files (x86) \visual Leak detector is my VLD installation directory. Add C:\Program Files (x86) \visual Leak Detector\lib\win32 in the Library directory, and note that the configuration items need to be separated by semicolons.

After the configuration is complete click the OK button to close the dialog, and then we need to introduce the header file # include <VLD.H> in the program code, but where is the header file introduced better? If it is an ordinary VC + + project where the introduction is irrelevant, but the Cocos2d-x project is different, we need to consider cross-platform, #include <vld.h> code should not be added to the classes directory in the H or CPP file, Files in this directory are to be compiled and run on other platforms, and # include <vld.h> is only valid on the Windrows platform. We can introduce the header file into the main.cpp or main.h file under the Win32 directory (see figure). These files are related to the Win32 platform and are not required when porting to different platforms.

If you introduce the following code in MAIN.CPP:

  1. #include "Main.h"
  2. #include "AppDelegate.h"
  3. #include "Cocos2d.h"
  4. #include <vld.h>
  5. USING_NS_CC;
  6. int Apientry _tWinMain (hinstance hinstance,
  7. HInstance hPrevInstance,
  8. LPTSTR lpCmdLine,
  9. int nCmdShow)
  10. {
  11. Unreferenced_parameter (hprevinstance);
  12. Unreferenced_parameter (lpCmdLine);
  13. Create the application instance
  14. Appdelegate app;
  15. Return Application::getinstance ()->run ();
  16. }

After the introduction, just test it, let's artificially create a memory leak, as in section 20.1.11, modify the code in HelloWorldScene.cpp:

    1. BOOL Helloworld::init ()
    2. {
    3. if (! Layer::init ())
    4. {
    5. return false;
    6. }
    7. __string *s = new __string ();
    8. Log ("%s", S->getcstring ());
    9. ... ...
    10. return true;
    11. }

To run the project, it is important to note that there is no stack output during the VLD of the program, but the log will have the output VLD installation information, the log information is as follows:

Visual Leak Detector Version 2.4rc2 installed.

Ready for GLSL

Ready for OpenGL 2.0

... ...

From the log, you can see whether the VLD installation was successful, and the version that was installed. You will not be outputting information in the log if you want to see the VLD detection report needing to exit the program. Using Cocos2d-x will output a lot of log information, the following information:

----------Block 526166 at 0x0821fa80:84 bytes----------

Leak hash:0x780b2033, count:1, total bytes

Call Stack (TID 4660):

... ...

----------Block 526214 at 0x08224378:8 bytes----------

Leak hash:0xe1dc1852, count:1, total 8 bytes

Call Stack (TID 4660):

... ...

Data:

6F (6F)---------cocos2d Autorele

6F 6F 6C XX CD CD CD CD ase.pool CD CD CD .....

Visual Leak Detector detected memory leaks (2892 bytes).

Largest number used:3204961 bytes.

Total allocations:69022415 bytes.

Visual Leak Detector is now exiting.

One block represents a memory leak point, and in many blocks if you can find log information about our own class? We can find the keyword "helloworldscene.cpp", which can locate the memory leak in the HelloWorld scene block, we found the following log information:

----------Block 1153 at 0x01533c70:48 bytes----------

Leak hash:0x5545a5ed, count:1, total bytes

Call Stack (TID 2088):

F:\dd\vctools\crt_bld\self_x86\crt\src\new.cpp ($): Msvcr110d.dll!operator New

D:\helloworld\classes\helloworldscene.cpp (+): helloworld.exe! Helloworld::init + 0x7 bytes

D:\helloworld\classes\helloworldscene.h (PNS): helloworld.exe! Helloworld::create + 0xb1 bytes

D:\helloworld\classes\helloworldscene.cpp (): helloworld.exe! Helloworld::createscene + 0x5 bytes

D:\helloworld\classes\appdelegate.cpp (+): helloworld.exe! appdelegate::applicationdidfinishlaunching + 0x5 bytes

D:\helloworld\cocos2d\cocos\2d\platform\win32\ccapplication.cpp (): Helloworld.exe!cocos2d::application::run + 0xF bytes

D:\helloworld\proj.win32\main.cpp (+): Helloworld.exe!wwinmain + 0xC bytes

F:\DD\VCTOOLS\CRT_BLD\SELF_X86\CRT\SRC\CRTEXE.C (528): Helloworld.exe!__tmaincrtstartup + 0x15 bytes

F:\DD\VCTOOLS\CRT_BLD\SELF_X86\CRT\SRC\CRTEXE.C (377): Helloworld.exe!wwinmaincrtstartup

0x7563850d (File and line number not available): KERNEL32. Dll! Basethreadinitthunk + 0xE bytes

0x77b7bf39 (File and line number not available): ntdll.dll! Rtlinitializeexceptionchain + 0x85 bytes

0X77B7BF0C (File and line number not available): ntdll.dll! Rtlinitializeexceptionchain + 0x58 bytes

Data:

1C 34 07 01 01 00 00 00 27 00 00 00 00 00 00 00.4 ... ‘.......

2 c A0, CD CD CD, CD, CD CD, 4...W. ........

CD CD CD CD cds CD CDS CD CD/DVD/0F 00 00 00 .......

From this log can see the memory leak point, from the log stack to find our own writing class, click on the line to open the Code window, locate the memory leak point code.

Locating memory leak points

Find out which one is likely to have a memory leak, the solution is not a problem.  

VisualStudio How to use visual Leak Detector

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.