{"id":218,"date":"2007-05-16T21:57:25","date_gmt":"2007-05-17T01:57:25","guid":{"rendered":"http:\/\/www.rakkar.org\/blog\/?p=218"},"modified":"2007-05-16T21:57:25","modified_gmt":"2007-05-17T01:57:25","slug":"build-in-memory-leak-detector","status":"publish","type":"post","link":"https:\/\/rakkar.org\/blog\/index.php\/2007\/05\/16\/build-in-memory-leak-detector\/","title":{"rendered":"Built-in memory leak detector"},"content":{"rendered":"<p>\t\t\t\tI didn&#8217;t know this until today but Visual Studio has a built-in memory leak detector.  One day out of the blue my game starts to take a long time to shut down and in the output window I see<\/p>\n<p><code>Detected memory leaks!<\/code><\/p>\n<p>Followed by a bunch of numbers. <\/p>\n<p>I looked it up today and after some research, I found out that the allocator counts memory allocations, and prints out the memory allocation number that caused the leak.  I don&#8217;t know why in all the time I&#8217;ve been programming I&#8217;ve never heard about this before but it&#8217;s really useful!<\/p>\n<p>This only works if your program state is repeatable (such as on startup) but it works really well from what I&#8217;ve seen.<\/p>\n<p>All you have to do is put<br \/>\n<code><br \/>\n#define _CRTDBG_MAP_ALLOC<br \/>\n#include  &lt;stdlib .h  &gt;<br \/>\n#include  &lt; crtdbg .h  &gt;<br \/>\n<\/code><\/p>\n<p>Somewhere at the top of your program.<\/p>\n<p>Then either put  at the start<\/p>\n<pre>\n_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );\n<\/pre>\n<p>or put<br \/>\n<code><br \/>\n_CrtDumpMemoryLeaks();<br \/>\n<\/code><\/p>\n<p>at the end.<\/p>\n<p>Lastly, to break at the specified allocation number, call<\/p>\n<p><code><br \/>\n_CrtSetBreakAlloc(X);<br \/>\n<\/code><\/p>\n<p>where X is the allocation number.<\/p>\n<p>Alternatively, put this in your watch window:<\/p>\n<p><code><br \/>\n{,,msvcr80d.dll}_crtBreakAlloc<br \/>\n<\/code><\/p>\n<p>It should show -1.  Change that -1 to the number of the allocation.<\/p>\n<p>For example:<\/p>\n<p><code><br \/>\nDetected memory leaks!<br \/>\nDumping objects -><br \/>\n{614841} normal block at 0x123A7D30, 8 bytes long.<br \/>\n Data: <> 7F 00 00 01 08 E6 CD CD<br \/>\n<\/code><\/p>\n<p>I put F10 to step one line into my program.<\/p>\n<p>I put {,,msvcr80d.dll}_crtBreakAlloc into the watch window.<\/p>\n<p>I change the -1 to 614841.<\/p>\n<p>I press F5 to run.<\/p>\n<p>It should break when it hits this allocation.<\/p>\n<p>I just fixed 2 leaks in about 5 minutes that I would have never found otherwise and only have 614,841 to go \ud83d\ude42\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t know this until today but Visual Studio has a built-in memory leak detector. One day out of the blue my game starts to take a long time to shut down and in the output window I see Detected memory leaks! Followed by a bunch of numbers. I looked it up today and after [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/218"}],"collection":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":0,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"wp:attachment":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}