{"id":1606,"date":"2022-08-03T00:25:58","date_gmt":"2022-08-03T00:25:58","guid":{"rendered":"https:\/\/writingagame.com\/?p=1606"},"modified":"2026-03-22T22:25:12","modified_gmt":"2026-03-22T22:25:12","slug":"chapter-41-memory-leak-detection","status":"publish","type":"post","link":"https:\/\/writingagame.com\/index.php\/2022\/08\/03\/chapter-41-memory-leak-detection\/","title":{"rendered":"Memory leaks detection"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Suddenly noticed that while running, memory consumption is slowly but steadily creeping up:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c41\/01.jpg\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>which can only mean one thing:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c41\/02.jpg\" alt=\"We have a &quot;memory leak&quot;.\"><\/p>\n\n\n\n<p>Well, then we need to find (detect) and fix it (or them). Some sort of memory leak detection tool would help.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Just in case, we are talking about <strong>Windows<\/strong> environment here.<\/li>\n<\/ul>\n\n\n\n<p>Visual Studio offers it&#8217;s own set of &#8220;CRT debug heap functions&#8221;. However, I was unable to get it to work. Then I found<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Visual Leak Detector<\/h2>\n\n\n\n<p>by <a href=\"https:\/\/github.com\/KindDragon\" target=\"_blank\" rel=\"noreferrer noopener\">KindDragon<\/a>, developed specifically for <strong>unmanaged C++<\/strong> (C#\/.NET will require another tools). You can download it <a href=\"https:\/\/kinddragon.github.io\/vld\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. It&#8217;s free. Works with VS 2022 just fine. Easy to install and to use. However, couple suggestions:<\/p>\n\n\n\n<p><strong>Installation<\/strong>. Though everything is pretty straight forward, still:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In order to keep your OS cleaner, better to change install location from default to your project, In my case &#8211; to <em>C:\\CPP\\p_windows\\Visual Leak Detector<\/em><\/li>\n\n\n\n<li>Don&#8217;t need to create a <em>Start Menu Folder<\/em><\/li>\n\n\n\n<li>Don&#8217;t really need to modify your environment, so UNcheck all 3 &#8220;Add VLD directory&#8221; options. Instead, we can specify DLL path in Project properties.<\/li>\n<\/ul>\n\n\n\n<p><strong>Deployment<\/strong>. We have to specify in the Project where to look for DLL and LIB libraries and for VLD header:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>.LIB: Go to <em>Project Properties -&gt; Linker -&gt; General.<\/em> Configuration: <strong>DEBUG<\/strong>. <em>Additional Library Directories -&gt; Edit<\/em>. Add New Line: <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>..\\..\\p_windows\\Visual Leak Detector\\lib\\Win32<\/code><\/pre>\n\n\n\n<p>(or x64? depending on your project)<\/p>\n\n\n\n<p>Ok &#8211; Apply &#8211; Ok<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>.DLL: Go to <em>Project Properties -&gt; Configuration Properties -&gt; Debugging.<\/em> Configuration: <strong>DEBUG<\/strong>. <em>Environment -&gt; arrow down -&gt; Edit<\/em>. Add a line:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>PATH=..\\..\\p_windows\\Visual Leak Detector\\bin\\Win32<\/code><\/pre>\n\n\n\n<p>(or x64?)<\/p>\n\n\n\n<p>Ok &#8211; Apply &#8211; Ok<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In <em>main.cpp<\/em> on the very top, <strong>before everything else<\/strong>, add<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"..\/..\/p_windows\/Visual Leak Detector\/include\/vld.h\"<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Build and run. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Just in case: Since now VLD registers and records all memory allocations, the initialization will take MUCH longer than usual, so be patient.<\/li>\n<\/ul>\n\n\n\n<p>Then stop. If you have memory leaks, they will be reported as<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c41\/03.jpg\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The beauty of VLD is that it shows specific line where leacked memory was allocated plus full call stack. Now, all you need &#8211; is to figure out where in your code you <em>supposed <\/em>to release it and why you didn&#8217;t.<\/p>\n\n\n\n<p>In my case, despite all expectations, instead of couple minor accidentally overlooked leaks, it revealed me a bunch (around 10) of serious <em>logical<\/em> discrepancies&#8230;<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c41\/04.jpg\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Took 2 days to fix them all. But now &#8211; <strong>No memory leaks detected<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"mb-2\">Suddenly noticed that while running, memory consumption is slowly but steadily creeping up: which can only mean one thing: Well, then we need to find (detect) and fix it (or them). Some sort of memory leak detection tool would help. Visual Studio offers it&#8217;s own set of &#8220;CRT debug heap functions&#8221;. However, I was unable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1621,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1606","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-side-notes"],"_links":{"self":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/1606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/comments?post=1606"}],"version-history":[{"count":41,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/1606\/revisions"}],"predecessor-version":[{"id":3591,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/1606\/revisions\/3591"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media\/1621"}],"wp:attachment":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media?parent=1606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/categories?post=1606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/tags?post=1606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}