{"id":3742,"date":"2018-03-07T11:50:57","date_gmt":"2018-03-07T19:50:57","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=3742"},"modified":"2018-03-07T11:50:57","modified_gmt":"2018-03-07T19:50:57","slug":"finding-memory-leaks-in-linux-programs-with-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/profiler\/linux\/leaks\/","title":{"rendered":"Finding Memory Leaks in Linux Programs with VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to find memory leaks in Linux programs using the VisualGDB integration with valgrind. Before you begin, install VisualGDB 5.3 or later.<\/p>\n<p>We will create a basic projectthat will randomly leak memory and will then use the\u00a0Dynamic Analysis feature of VisualGDB to\u00a0automatically run the program under Valgrind and produce a detailed report on memory leaks.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Linux Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3743\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/01-prj.png\" alt=\"01-prj\" width=\"786\" height=\"507\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/01-prj.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/01-prj-300x194.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project&#8221; -&gt; &#8220;Application&#8221; -&gt; &#8220;MSBuild&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-app1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3744\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-app1.png\" alt=\"02-app\" width=\"886\" height=\"693\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-app1.png 886w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-app1-300x235.png 300w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/a><\/li>\n<li>Select the Linux computer you would like to target. VisualGDB will automatically check that it has all necessary tools and will install the missing ones:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/03-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3745\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/03-target.png\" alt=\"03-target\" width=\"886\" height=\"693\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/03-target.png 886w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/03-target-300x235.png 300w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/a><\/li>\n<li>Proceed with the default option of keeping all source files on the Windows machine and uploading them during builds:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/04-disp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3746\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/04-disp.png\" alt=\"04-disp\" width=\"886\" height=\"693\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/04-disp.png 886w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/04-disp-300x235.png 300w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create your project.\u00a0Replace the contents of\u00a0the main source file with the following code\u00a0demonstrating memory leaks:\n<pre class=\"\">#include &lt;iostream&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string&gt;\r\n\r\ntemplate &lt;int X&gt; void AllocAndMaybeFree()\r\n{\r\n    char *pTest = new char[X];\r\n    if (rand() % 5)\r\n        delete[] pTest;\r\n}\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    AllocAndMaybeFree&lt;1&gt;();\r\n    AllocAndMaybeFree&lt;2&gt;();\r\n    AllocAndMaybeFree&lt;3&gt;();\r\n    AllocAndMaybeFree&lt;4&gt;();\r\n    AllocAndMaybeFree&lt;5&gt;();\r\n    AllocAndMaybeFree&lt;6&gt;();\r\n    AllocAndMaybeFree&lt;7&gt;();\r\n    AllocAndMaybeFree&lt;8&gt;();\r\n    AllocAndMaybeFree&lt;9&gt;();\r\n    AllocAndMaybeFree&lt;10&gt;();\r\n    return 0;\r\n}<\/pre>\n<p>In real-world scenarios the memory is allocated in multiple different\u00a0parts\u00a0of\u00a0the program, and if some of those allocations are not released, the memory leaks. We\u00a0are simulating this with the\u00a0<strong>AllocAndMaybeFree()<\/strong> function that will &#8220;forget&#8221; to free the allocated memory block with a 20% probability. We use the template argument to make different invocations\u00a0of the function appear differently\u00a0in the call stack, as if we had 10 distinct functions potentially leaking memory. Build the project\u00a0and ensure\u00a0is results in no errors:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/05-code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3747\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/05-code.png\" alt=\"05-code\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/05-code.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/05-code-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/05-code-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Now we will show how to run the program under Valgrind. Open VisualGDB Project Properties, go to the Dynamic Analysis page and enable the &#8220;Analyze program behavior during runtime&#8221; checkbox:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-analyze.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3748\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-analyze.png\" alt=\"06-analyze\" width=\"786\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-analyze.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-analyze-300x227.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Press OK and start debugging your program.\u00a0VisualGDB will automatically launch valgrind\u00a0and attach gdb to it. You will see the diagnostic output in the Valgrind Diagnostics window, but the rest of the\u00a0debugging experience will be the same as if you were debugging your program regularly:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3749\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-debug.png\" alt=\"07-debug\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-debug.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-debug-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-debug-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Once the program exits, Valgrind will display the summary of the\u00a0discovered memory leaks:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-summary.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3750\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-summary.png\" alt=\"08-summary\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-summary.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-summary-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-summary-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties again and switch the &#8220;Memory leaks&#8221; setting to &#8220;Full list&#8221;:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-full.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3751\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-full.png\" alt=\"09-full\" width=\"786\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-full.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-full-300x227.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Run the program again. Valgrind will now report full call stacks of the allocations that were never freed, showing how calls #4 and #6 resulted in memory leaks:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-stacks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3752\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-stacks.png\" alt=\"10-stacks\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-stacks.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-stacks-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-stacks-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>You can also\u00a0analyze the leaks\u00a0in real time using the GDB Session window in VisualGDB. Simply run the &#8220;mon leak_check&#8221; command while debugging under valgrind:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-check.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3753\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-check.png\" alt=\"11-check\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-check.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-check-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-check-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Valgrind will output the summary of the current allocations:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-initial.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3754\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-initial.png\" alt=\"12-initial\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-initial.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-initial-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-initial-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>If you re-run the command after\u00a0a memory block is allocated, not freed and not pointed by any other block, the &#8220;mon leak_check&#8221; command will report it as &#8220;definitely lost&#8221; showing the allocation stack:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/13-new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3755\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/13-new.png\" alt=\"13-new\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/13-new.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/13-new-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/13-new-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Valgrind will also automatically handle references between the blocks. We will demonstrate this by\u00a0allocating an instance of a class that includes an STL string. Replace the main file contents with the following code:\n<pre class=\"\">#include &lt;iostream&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string&gt;\r\n\r\nusing namespace std;\r\n\r\nclass DemoClass\r\n{\r\npublic:\r\n    std::string Test = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\";\r\n};\r\n\r\nvoid func()\r\n{\r\n    DemoClass *p = new DemoClass();\r\n}\r\n \r\nint main(int argc, char *argv[])\r\n{\r\n    func();\r\n    return 0;\r\n}<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/14-class.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3756\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/14-class.png\" alt=\"14-class\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/14-class.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/14-class-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/14-class-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Set\u00a0a breakpoint in the func() function and start debugging. Once the breakpoint is hit, run the &#8220;mon leak_check&#8221; command (use the &#8216;up&#8217; button in the GDB Session window to repeat the previous\u00a0command instead of re-typing it):<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/15-reach.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3757\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/15-reach.png\" alt=\"15-reach\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/15-reach.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/15-reach-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/15-reach-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<li>Step over the allocation line and re-run the &#8220;mon leak_check&#8221; command: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/16-reach2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3758\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/16-reach2.png\" alt=\"16-reach2\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/16-reach2.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/16-reach2-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/16-reach2-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a>Note how the &#8220;still reachable&#8221; blocks have grown by 2 and no leak is reported yet. This happens because the &#8216;p&#8217; variable still points to the newly allocated memory and valgrind assumes it might be freed later.<\/li>\n<li>Step out of the func() function and re-run &#8220;mon leak_check&#8221;. Now valgrind will\u00a0report a memory leak. Note how the memory block allocated by the STL string is\u00a0not reported separately (it is\u00a0 shown as +1) as it is only referenced by the already reported block:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/17-leak.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3759\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/17-leak.png\" alt=\"17-leak\" width=\"1108\" height=\"735\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/17-leak.png 1108w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/17-leak-300x199.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/17-leak-1024x679.png 1024w\" sizes=\"(max-width: 1108px) 100vw, 1108px\" \/><\/a><\/li>\n<\/ol>\n<p>VisualGDB also provides a convenient way to analyze your program performance using Valgrind.\u00a0See our <a href=\"https:\/\/visualgdb.com\/tutorials\/profiler\/linux\/\">Linux\u00a0Profiling Tutorial<\/a> for a detailed step-by-step guide.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to find memory leaks in Linux programs using the VisualGDB integration with valgrind. Before you begin,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[33,109,153],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3742"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=3742"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3742\/revisions"}],"predecessor-version":[{"id":3760,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3742\/revisions\/3760"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=3742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=3742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=3742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}