[{"data":1,"prerenderedAt":137},["ShallowReactive",2],{"\u002F2013-07-20-vc%E4%B8%8A%E6%A3%80%E6%B5%8B%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2%E7%9A%84%E7%A5%9E%E5%99%A8-vld":3},{"id":4,"title":5,"body":6,"date":115,"description":12,"extension":116,"meta":117,"navigation":120,"path":132,"seo":133,"stem":134,"tags":135,"__hash__":136},"blogs\u002F_legacy\u002F2013\u002F2013-07-20-vc%e4%b8%8a%e6%a3%80%e6%b5%8b%e5%86%85%e5%ad%98%e6%b3%84%e9%9c%b2%e7%9a%84%e7%a5%9e%e5%99%a8-vld.md","VC上检测内存泄露的神器——vld",{"type":7,"value":8,"toc":111},"minimark",[9,13,24,27,30,37,40,51,54,102,105],[10,11,12],"p",{},"前段在q群里聊天，发现有的童鞋和原来的我一样，不知道怎么去检查代码中的内存泄露。",[10,14,15,16,23],{},"对于我们新手而言，内存泄露的问题其实要到程序准备发布的时候才会察觉到。虽然平时总是会注意到这些问题，但是一旦项目大过某个程度，自然会出现一些无法顾及的地方。内存总是不经意的就泄露了，看着程序边跑内存边增加，着实是一件让人头痛的事情。所以这厢就来介绍下vc下检测内存泄露的优秀工具：",[17,18,22],"a",{"href":19,"rel":20},"https:\u002F\u002Fvld.codeplex.com\u002Freleases",[21],"nofollow","Visual Leak Detector","。",[10,25,26],{},"Visual Leak Detector其实是基于vc自带的检测工具工作的，虽然知道原理的情况下我们自己也可以做出一个功能类似的东西来。但是又何必再造车轮呢？vld本身是开源项目，想要知道其实现的童鞋可以看看它的代码哦。大致上似乎是基于VC++DEBUGER和CRT库提供的检测和定位内存泄漏的工具，不过作为一个有历史的开源项目，其功能自然没有简单到这种程度。",[10,28,29],{},"vld简单易用，文档也很丰富，对于内存泄露的具体位置也能以调用堆栈的形式详细的显示出来。在代码上的变动只需要简单的加上#include 就可以了。集成起来也很方便，与其他的第三方库没有区别，而且它本身只工作在debug模式，release的时候根本就不会编译，相当之便利。不知道如何配置的童鞋可以参考下面的来做，注意更改成自己的vld安装路径就好：",[10,31,32],{},[33,34],"img",{"alt":35,"src":36},"vld","\u002Fwp-content\u002Fuploads\u002F2013\u002F07\u002Fvld-300x111.jpg",[10,38,39],{},"使用下面的简单代码就可以看到vld是不是在工作：",[41,42,47],"pre",{"className":43,"code":45,"language":46},[44],"language-text","#include \"stdafx.h\"\n#include \u003Cvld.h>\n\nclass LeakTest\n{\nprivate:\n    int a;\n    float b;\npublic:\n    LeakTest(){\n        a = 4;\n        b = 3.9;\n    }\n};\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n\n    LeakTest* lt = new LeakTest();\n    return 0;\n}\n","text",[48,49,45],"code",{"__ignoreMap":50},"",[10,52,53],{},"运行之后vs的输出栏下就会有vld的检测结果，类似下面的文字：",[55,56,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99],"blockquote",{},[10,58,59],{},"WARNING: Visual Leak Detector detected memory leaks!",[10,61,62],{},"---------- Block 1 at 0x003AFFD8: 8 bytes ----------",[10,64,65],{},"Call Stack:",[10,67,68],{},"d:\\documents\\visual studio 2012\\projects\\consoleapplication1\\consoleapplication1\\consoleapplication1.cpp (22): ConsoleApplication1.exe!wmain + 0x7 bytes",[10,70,71],{},"f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\crtexe.c (533): ConsoleApplication1.exe!__tmainCRTStartup + 0x19 bytes",[10,73,74],{},"f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\crtexe.c (377): ConsoleApplication1.exe!wmainCRTStartup",[10,76,77],{},"0x7576ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes",[10,79,80],{},"0x76EE377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes",[10,82,83],{},"0x76EE374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes",[10,85,86],{},"Data:",[10,88,89],{},"04 00 00 00 9A 99 79 40 ......y@ ........",[10,91,92],{},"Visual Leak Detector detected 1 memory leak (44 bytes).",[10,94,95],{},"Largest number used: 44 bytes.",[10,97,98],{},"Total allocations: 44 bytes.",[10,100,101],{},"Visual Leak Detector is now exiting.",[10,103,104],{},"相当的好用吧！",[10,106,107,108],{},"vld的最新版可以到官网下载：",[17,109,19],{"href":19,"rel":110},[21],{"title":50,"searchDepth":112,"depth":113,"links":114},2,3,[],"2013-07-20","md",{"layout":118,"status":119,"published":120,"author":121,"author_login":122,"author_email":123,"author_url":124,"wordpress_id":125,"wordpress_url":126,"date_gmt":127,"excerpt":128},"post","publish",true,{"display_name":122,"login":122,"email":123,"url":124},"chaoshikari","chaoshikari@gmail.com","\u002F",595,"\u002F\u002F?p=595","2013-07-20 02:41:53 +0000",{"type":7,"value":129},[130],[10,131,12],{},"\u002F2013-07-20-vc上检测内存泄露的神器-vld",{"title":5,"description":12},"_legacy\u002F2013\u002F2013-07-20-vc%e4%b8%8a%e6%a3%80%e6%b5%8b%e5%86%85%e5%ad%98%e6%b3%84%e9%9c%b2%e7%9a%84%e7%a5%9e%e5%99%a8-vld",[],"1_tgUlUxzC9CSiWJNwiOtD43Zre7saQg_aXaPUsOA80",1788763183269]