{"id":237,"date":"2017-05-11T03:21:52","date_gmt":"2017-05-11T03:21:52","guid":{"rendered":"http:\/\/imalogic.com\/blog\/?p=237"},"modified":"2020-09-28T10:04:50","modified_gmt":"2020-09-28T10:04:50","slug":"how-to-hunt-memory-leaks-using-visual-studio","status":"publish","type":"post","link":"https:\/\/imalogic.com\/blog\/2017\/05\/11\/how-to-hunt-memory-leaks-using-visual-studio\/","title":{"rendered":"How to hunt memory leaks using Visual Studio"},"content":{"rendered":"<body><p>Make sure you compile debug version of your project.<\/p>\n<div class=\"entry\">\n<p>Press F5 (Start Debugging)<\/p>\n<p>Do whatever you suspect to leak memory, and close program.<\/p>\n<p>In output window you should see:<\/p>\n<p><code>Detected memory leaks!<br>\nDumping objects -&gt;<br>\n{8677} normal block at 0x01AA4E08, 68 bytes long.<br>\nData: &lt; &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD<br>\nObject dump complete.<\/code><\/p>\n<p>Add:<br><code><br>\n#ifdef _DEBUG<br>\n#define _CRTDBG_MAP_ALLOC<br>\n#define _INC_MALLOC<br>\n#endif<br><\/code><\/p>\n<p>in your stdafx.h file, and recompile whole solution.<\/p>\n<p>Now it should be something like:<\/p>\n<p><code>Detected memory leaks!<br>\nDumping objects -&gt;<br>\nC:\\Program Files\\Microsoft Visual Studio 8\\VC\\include\\crtdbg.h(1147) : {8677} normal block at 0x01AA4E08, 68 bytes long.<br>\nData: &lt; &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD<br>\nObject dump complete.<\/code><\/p>\n<p>The problem is it shows allocator code, not your allocation itself. Still not very usefull. Now tricky part. Double click<\/p>\n<p><em>C:\\Program Files\\Microsoft Visual Studio 8\\VC\\include\\crtdbg.h\u00a0<\/em>line.<\/p>\n<p>Set a trap (F9\/Debug-&gt;Toggle Breakpoint) right click on the red dot and select condition and type <em>_Size==&lt;your leak size in this case 68&gt;<\/em>. Next time you will debug, program will stop if there will be 68 bytes allocated, then you can select <em>Debug-&gt;Windows-&gt;Call Stack<\/em> and if you are lucky you will see your memory leak.<\/p>\n<\/div>\n<h1>Hunt memory leaks using Visual Studio &amp; Deleaker<\/h1>\n<p>Another way to hunt memory leaks is using a useful tools called <a href=\"https:\/\/www.deleaker.com\/\"><strong>Deleaker.<\/strong><\/a><\/p>\n<p>Deleaker is a plugin for Visual Studio, when you enable it, you have access to the deleaker windows.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"861\" data-permalink=\"https:\/\/imalogic.com\/blog\/2017\/05\/11\/how-to-hunt-memory-leaks-using-visual-studio\/img_8394\/\" data-orig-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?fit=2016%2C1512&amp;ssl=1\" data-orig-size=\"2016,1512\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;1.8&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone X&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601292063&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4&quot;,&quot;iso&quot;:&quot;32&quot;,&quot;shutter_speed&quot;:&quot;0.0416666666667&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"IMG_8394\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?fit=810%2C608&amp;ssl=1\" class=\"alignnone wp-image-861 size-large\" src=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394.jpg?resize=810%2C608&#038;ssl=1\" alt=\"\" width=\"810\" height=\"608\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?resize=1536%2C1152&amp;ssl=1 1536w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?resize=880%2C660&amp;ssl=1 880w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?w=2016&amp;ssl=1 2016w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8394-rotated.jpg?w=1620&amp;ssl=1 1620w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"859\" data-permalink=\"https:\/\/imalogic.com\/blog\/2017\/05\/11\/how-to-hunt-memory-leaks-using-visual-studio\/img_8392\/\" data-orig-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?fit=2016%2C1512&amp;ssl=1\" data-orig-size=\"2016,1512\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;1.8&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone X&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601292017&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4&quot;,&quot;iso&quot;:&quot;125&quot;,&quot;shutter_speed&quot;:&quot;0.00909090909091&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"IMG_8392\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?fit=810%2C608&amp;ssl=1\" class=\"alignnone wp-image-859 size-large\" src=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392.jpg?resize=810%2C608&#038;ssl=1\" alt=\"\" width=\"810\" height=\"608\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?resize=1536%2C1152&amp;ssl=1 1536w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?resize=880%2C660&amp;ssl=1 880w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?w=2016&amp;ssl=1 2016w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8392-rotated.jpg?w=1620&amp;ssl=1 1620w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/a><\/p>\n<p>When you perform a execution of your program in debug, deleaker start to analyse your code and provide you all information about memory leaks.<\/p>\n<p>I have made a basic test with a array of object without deleting all created object. Deleaker have detected all unreleased objects ( 10 Hits ).<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"858\" data-permalink=\"https:\/\/imalogic.com\/blog\/2017\/05\/11\/how-to-hunt-memory-leaks-using-visual-studio\/img_8391\/\" data-orig-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?fit=2016%2C1512&amp;ssl=1\" data-orig-size=\"2016,1512\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;1.8&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone X&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601291967&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4&quot;,&quot;iso&quot;:&quot;32&quot;,&quot;shutter_speed&quot;:&quot;0.0333333333333&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"IMG_8391\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?fit=810%2C608&amp;ssl=1\" class=\"alignnone wp-image-858 size-large\" src=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391.jpg?resize=810%2C608&#038;ssl=1\" alt=\"\" width=\"810\" height=\"608\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?resize=1536%2C1152&amp;ssl=1 1536w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?resize=880%2C660&amp;ssl=1 880w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?w=2016&amp;ssl=1 2016w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8391-rotated.jpg?w=1620&amp;ssl=1 1620w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"860\" data-permalink=\"https:\/\/imalogic.com\/blog\/2017\/05\/11\/how-to-hunt-memory-leaks-using-visual-studio\/img_8393\/\" data-orig-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?fit=2016%2C1512&amp;ssl=1\" data-orig-size=\"2016,1512\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;1.8&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone X&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601292047&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4&quot;,&quot;iso&quot;:&quot;40&quot;,&quot;shutter_speed&quot;:&quot;0.030303030303&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"IMG_8393\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?fit=810%2C608&amp;ssl=1\" class=\"alignnone wp-image-860 size-large\" src=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393.jpg?resize=810%2C608&#038;ssl=1\" alt=\"\" width=\"810\" height=\"608\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?resize=1536%2C1152&amp;ssl=1 1536w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?resize=880%2C660&amp;ssl=1 880w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?w=2016&amp;ssl=1 2016w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8393-rotated.jpg?w=1620&amp;ssl=1 1620w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/a><\/p>\n\n\n<p>The screenshot above show when i release correctly all objects. Deleaker do the job \ud83d\ude42<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"810\" height=\"608\" data-attachment-id=\"862\" data-permalink=\"https:\/\/imalogic.com\/blog\/2017\/05\/11\/how-to-hunt-memory-leaks-using-visual-studio\/img_8395\/\" data-orig-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?fit=2016%2C1512&amp;ssl=1\" data-orig-size=\"2016,1512\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;1.8&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone X&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601292076&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4&quot;,&quot;iso&quot;:&quot;40&quot;,&quot;shutter_speed&quot;:&quot;0.0416666666667&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"IMG_8395\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?fit=810%2C608&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395.jpg?resize=810%2C608&#038;ssl=1\" alt=\"\" class=\"wp-image-862\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?resize=1536%2C1152&amp;ssl=1 1536w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?resize=880%2C660&amp;ssl=1 880w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?w=2016&amp;ssl=1 2016w, https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/IMG_8395-rotated.jpg?w=1620&amp;ssl=1 1620w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/a><\/figure>\n\n\n\n<p>Above, the deleaker configuration options\u2026<\/p>\n\n\n\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>Make sure you compile debug version of your project. Press F5 (Start Debugging) Do whatever you suspect to leak memory,<\/p>\n","protected":false},"author":1,"featured_media":304,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,66,2,1],"tags":[24,35,32,34,33],"class_list":["post-237","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-computer-graphics","category-demo","category-uncategorized","tag-c","tag-debug","tag-memory-leaks","tag-microsoft","tag-visual-studio"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/imalogic.com\/blog\/wp-content\/uploads\/2017\/05\/memoty_leak_android.jpg?fit=690%2C335&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8J21V-3P","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":2,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/posts\/237\/revisions\/867"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/media\/304"}],"wp:attachment":[{"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imalogic.com\/blog\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}