{"id":5653,"date":"2013-01-03T07:00:00","date_gmt":"2013-01-03T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/01\/03\/why-does-my-program-run-really-slow-or-even-crash-or-stop-crashing-or-crash-differently-if-running-under-a-debugger\/"},"modified":"2013-01-03T07:00:00","modified_gmt":"2013-01-03T07:00:00","slug":"why-does-my-program-run-really-slow-or-even-crash-or-stop-crashing-or-crash-differently-if-running-under-a-debugger","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130103-00\/?p=5653\/","title":{"rendered":"Why does my program run really slow or even crash (or stop crashing, or crash differently) if running under a debugger?"},"content":{"rendered":"<p>More than once, a customer has noticed that running the exact same program under the debugger rather than standalone causes it to change behavior. And not just in the &#8220;oh, the timing of various operations changed to hit different race conditions&#8221; but in much more fundamental ways like &#8220;my program runs really slow&#8221; or &#8220;my program crashes in a totally different location&#8221; or (even more frustrating) &#8220;<a href=\"http:\/\/blogs.msdn.com\/b\/larryosterman\/archive\/2008\/09\/03\/anatomy-of-a-heisenbug.aspx\">my bug goes away<\/a>&#8220;.<\/p>\n<p> What&#8217;s going on? I&#8217;m not even switching between the retail and debug versions of my program, so I&#8217;m not a victim of <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/08\/15\/701130.aspx\"> changing program semantics in the debug build<\/a>. <\/p>\n<p> When a program is running under the debugger, some parts of the system behave differently. One example is that the <code>Close&shy;Handle<\/code> function raises an exception (I believe it&#8217;s <code>STATUS_INVALID_HANDLE<\/code> but don&#8217;t quote me) if you ask it to close a handle that isn&#8217;t open. But the one that catches most people is that when run under the debugger, an alternate heap is used. This alternate heap has a different memory layout, and it does extra work when allocating and freeing memory to help try to catch common heap errors, like filling newly-allocated memory with a known sentinel value. <\/p>\n<p> But this change in behavior can make your debugging harder or impossible. <\/p>\n<p> So much for people&#8217;s suggestions to <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/01\/11\/9946339.aspx\"> switch to a stricter implementation of the Windows API when a debugger is attached<\/a>. <\/p>\n<p> On Windows&nbsp;XP and higher, you can <a href=\"http:\/\/msdn.microsoft.com\/library\/ff538841.aspx\"> disable the debug heap even when debugging<\/a>. If you are using a <code>dbgeng<\/code>-based debugger like <code>ntsd<\/code> or <code>WinDbg<\/code>, you can pass the <code>-hd<\/code> command line switch. If you are using Visual Studio, you can <a href=\"http:\/\/mackeblog.blogspot.com\/2009\/02\/disabling-visual-studios-debugger-heap.html\"> set the <code>_NO_DEBUG_HEAP<\/code> environment variable to <code>1<\/code><\/a>. <\/p>\n<p> If you are debugging on a version of Windows prior to Windows&nbsp;XP, you can start the process without a debugger, then connect a debugger to the live process. The decision to use the debug heap is made at process startup, so connecting the debugger afterwards ensures that the retail heap is chosen. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>More than once, a customer has noticed that running the exact same program under the debugger rather than standalone causes it to change behavior. And not just in the &#8220;oh, the timing of various operations changed to hit different race conditions&#8221; but in much more fundamental ways like &#8220;my program runs really slow&#8221; or &#8220;my [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-5653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>More than once, a customer has noticed that running the exact same program under the debugger rather than standalone causes it to change behavior. And not just in the &#8220;oh, the timing of various operations changed to hit different race conditions&#8221; but in much more fundamental ways like &#8220;my program runs really slow&#8221; or &#8220;my [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=5653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}