Introduction to performance optimization tools in Cocos development-Visual Studio Memory leakage detection tools-Visual Leak Detector and cocosleak

Source: Internet
Author: User

Introduction to performance optimization tools in Cocos development-Visual Studio Memory leakage detection tools-Visual Leak Detector and cocosleak
So what kind of good memory leak detection tools are available in Windows? Microsoft provides Visual Studio development tools without any good memory Leak detection function. We can use a third-party tool Visual Leak Detector (vld ).

Vld is a small, easy-to-use, free and open-source memory leak detection tool in the VC ++ environment. vld can display the complete memory allocation call stack that causes memory leakage. The vld detection report provides complete stack trace for each memory leak point and contains its source file and row number information.

The installation process is to first download the vldinstallation file at the address http://vld.codeplex.com/. then, install the installation program to configure the environment variable. Remember the installation directory.

After the installation is complete, open the Visual Studio project to be detected. We need to configure the vld header file directory and the vld library directory in the project.

Select a game project, open the "project"> "properties" dialog box, and select "configuration properties"> "VC ++ directory"> "General ", add C: \ Program Files (x86) \ Visual Leak Detector \ include in the "contain directory" on the right, where C: \ Program Files (x86) \ Visual Leak Detector is my vld installation directory. Add C: \ Program Files (x86) \ Visual Leak Detector \ lib \ Win32 to "library directory". Note that the configuration items must be separated by semicolons.

 

After the configuration is complete, click "OK" to close the dialog box. Then we need to introduce the header file # include <vld. h> in the program code. But where can I introduce this header file? If it is a common VC ++ project where the introduction does not matter, but the Cocos2d-x project is different, we need to consider cross-platform, # include <vld. h> the code should not be added to the h or cpp file in the Classes directory. The files in this directory should be compiled and run on other platforms, and # include <vld. h> it is only valid on the Windrows platform. We can introduce the header file 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 migration from different platforms.

 

If the following code is introduced in main. cpp:

#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 will test it. We will create a memory leak manually, and modify the code in HelloWorldScene. cpp in the same way as in segment 1.1:

bool HelloWorld::init(){if ( !Layer::init() ){return false;}    __String *s = new __String();     log("%s",s->getCString()); … … return true;}

When running a project, note that the vld does not have stack output while the program is running, but the log will output the vld installation information. The log information is as follows:

Visual Leak Detector Version 2.4RC2 installed.

Ready for GLSL

Ready for OpenGL 2.0

... ...

The log shows whether the vld is successfully installed and the installed version. To view the vld detection report, you must exit the program before outputting information in the log. Using a Cocos2d-x will output a lot of log information as follows:

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

Leak Hash: 0x780B2033, Count: 1, Total 84 bytes

Call Stack (TID 4660 ):

......

 

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

Leak Hash: 0xE1DC1852, Count: 1, Total 8 bytes

Call Stack (TID 4660 ):

......

Data:

63 6F 63 6F 73 32 64 20 61 75 74 6F 72 65 6C 65 cocos2d. autorele

61 73 65 20 70 6F 6F 6C 00 CD ase. pool ........

 

Visual Leak Detector detected 33 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. What if we can find log information about our own classes in many blocks? We can search for the keyword "helloworldscene. cpp" to locate the memory leakage Block in the HelloWorld scenario. We can find the following log information:

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

Leak Hash: 0x5545A5ED, Count: 1, Total 48 bytes

Call Stack (TID 2088 ):

F: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ new. cpp (57): MSVCR110D. dll! Operator new

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

D: \ helloworld \ classes \ helloworldscene. h (37): HelloWorld.exe! HelloWorld: create + 0xB1 bytes

D: \ helloworld \ classes \ helloworldscene. cpp (12): HelloWorld.exe! HelloWorld: createScene + 0x5 bytes

D: \ helloworld \ classes \ appdelegate. cpp (30): HelloWorld.exe! AppDelegate: applicationDidFinishLaunching + 0x5 bytes

D: \ helloworld \ cocos2d \ cocos \ 2d \ platform \ win32 \ ccapplication. cpp (74): HelloWorld.exe! Cocos2d: Application: run + 0xF bytes

D: \ helloworld \ proj. win32 \ main. cpp (19): 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 00 27 00 00 00 00 00 00. 4 ......'.......

2C 34 07 01 A0 77 01 03 00 CD, 4 ...... w ..........

CD 00 00 00 00 0F 00 00 00 ................

 

From this log, we can see the memory leak point, find the class we have compiled from the log stack, click the line to open the code window, and locate the memory leak point code, as shown in.

 

Locating memory leaks

 

Find out which one may have memory leakage, and the solution is not a problem.



More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.c Ocoagame.net
For more exciting video courses, please follow the Cocos course in Zhijie class: http: // v.51w Ork6.com
Welcome to the Cocos2d-x Technology Discussion Group: 257760386 welcome to the knowledge of the iOS classroom public platform

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.