Visual leak detector Check for memory leaks _c language

Source: Internet
Author: User
Tags stack trace win32

So what's a good memory leak detection tool under Windows? Microsoft provides Visual Studio development tools that do not have 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 the VC + + environment under a small easy-to-use, free open source of memory leak detection tool, VLD can show the memory leak caused by the full memory allocation call stack. The VLD Detection report provides a complete stack trace for each memory leak point and contains its source file and line number information.

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

When 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, as shown in the figure, select "Configuration Properties" → "VC + + Directory" → "General", in the right "include directory" add C:\Program Files (x86) \visual leak detector \include, where C:\Program Files (x86) \visual leak detector is my VLD installation directory. Add C:\Program Files (x86) \visual leak detector\lib\win32 to the library directory, noting that the configuration needs to be separated by semicolons.

After the configuration is complete, click the OK button to close the dialog box, and then we need to introduce the header file #include <vld.h> in the program code, but where does this header file come in better? If it is a common VC + + project where the introduction does not matter, but Cocos2d-x project is different, we need to consider Cross-platform, #include <vld.h> code should not be added to the classes directory of H or CPP files, The files in this directory are to be compiled on other platforms, and #include <vld.h> is only available on the Windrows platform. We can introduce header files in 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 for porting to different platforms.

If the code is introduced in Main.cpp, the following:

#include "main.h" 
#include "AppDelegate.h" 
#include "cocos2d.h" 
  
#include <vld.h> 
  
using_ns_cc ; 
  
int Apientry _tWinMain (hinstance hinstance, 
            hinstance hprevinstance, 
            LPTSTR lpcmdline  , 
            int    nCmdShow) 
{ 
  unreferenced_parameter (hprevinstance); 
  Unreferenced_parameter (lpcmdline); 
  
  Create the application instance 
  appdelegate app; 
  Return Application::getinstance ()->run (); 
} 

After the introduction, we tested it, and we created a memory leak that, like 20.1.11, modifies the code in HelloWorldScene.cpp:

BOOL Helloworld::init () 
{ 
if (! Layer::init ()) 
{return 
false; 
} 
  __string *s = new __string (); 
  
  Log ("%s", s->getcstring ()); 
  
..... return true; 
} 

To run the project, it is necessary to note that in the process of running the program VLD is not the stack output, but the log will have output VLD installation information, log information as follows:

Visual Leak detector Version 2.4RC2 installed.

Ready for GLSL

Ready for OpenGL 2.0

... ...

You can see from the log whether the VLD was installed successfully and the version installed. You will not be able to output the information in the log until you see that the VLD test report needs to exit the program. Using Cocos2d-x will output a lot of log information as follows:

----------block 526166 in 0x0821fa80:84 bytes----------

 leak hash:0x780b2033, count:1, total-bytes call

 St ACK (TID 4660):

...

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

 leak hash:0xe1dc1852-count:1, total 8 bytes Call

 Stac K (TID 4660): ...

 Data:

  6F 6F m-   cocos2d 6F-Autorele 6C----  70 6 F 6F 6C CD CD CD CD CD CD   ase.pool

... 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, what if you can find log information about our own class? We can find the keyword "helloworldscene.cpp", which can be located in the HelloWorld scene of the memory leak block, we found the following log information:

----------Block 1153 at the 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\hel Loworldscene.cpp (): helloworld.exe! Helloworld::init + 0x7 bytes d:\helloworld\classes\helloworldscene.h (Panax notoginseng): helloworld.exe! Helloworld::create + 0xb1 bytes d:\helloworld\classes\helloworldscene.cpp (a): 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 (): H Elloworld.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 A0 a CD CD CD CD CD CD, 4...W.

  ........

 CD CD CD CDs CD CD CD CDS CD CDs 00 00 00 ..........

From this log can see the memory leak point, from the log stack to find our own written class, click on that line open the Code window, locate the memory leak point code, as shown in the figure.

Locating a memory leak point

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

The above mentioned is the entire content of this article, I hope you can enjoy.

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.