title: How to debug a buffer overrun in Visual C++ 9?
body: <p>I have a huge MMC snapin written in Visual C++ 9. Every once in a while when I hit F5 in MMC mmc.exe crashes. If I attach a debugger to it I see the following message:</p>
  <blockquote>
  <p>A buffer overrun has occurred in mmc.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.</p>
  <p>For more details please see Help topic 'How to debug Buffer Overrun Issues'.</p>
  </blockquote>
  <p>First of all, there's no <em>How to debug Buffer Overrun Issues</em> topic anywhere.</p>
  <p>When I inspect the call stack I see that it's likely something with security cookies used to guard against stack-allocated buffer overruns:</p>
  <pre><code>MySnapin.dll!__crt_debugger_hook()  Unknown
  MySnapin.dll!__report_gsfailure()  Line 315 + 0x7 bytes C
  mssvcr90d.dll!ValidateLocalCookies(void (unsigned int)* CookieCheckFunction=0x1014e2e3, _EH4_SCOPETABLE * ScopeTable=0x10493e48, char * FramePointer=0x0007ebf8)  + 0x57 bytes  C
  msvcr90d.dll!_except_handler4_common(unsigned int * CookiePointer=0x104bdcc8, void (unsigned int)* CookieCheckFunction=0x1014e2e3, _EXCEPTION_RECORD * ExceptionRecord=0x0007e764, _EXCEPTION_REGISTRATION_RECORD * EstablisherFrame=0x0007ebe8, _CONTEXT * ContextRecord=0x0007e780, void * DispatcherContext=0x0007e738)  + 0x44 bytes    C
  MySnapin.dll!_except_handler4(_EXCEPTION_RECORD * ExceptionRecord=0x0007e764, _EXCEPTION_REGISTRATION_RECORD * EstablisherFrame=0x0007ebe8, _CONTEXT * ContextRecord=0x0007e780, void * DispatcherContext=0x0007e738)  + 0x24 bytes C
  ntdll.dll!7c9032a8()    
  [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
  ntdll.dll!7c90327a()    
  ntdll.dll!7c92aa0f()    
  ntdll.dll!7c90e48a()    
  MySnapin.dll!IComponentImpl&lt;CMySnapin&gt;::GetDisplayInfo(_RESULTDATAITEM * pResultDataItem=0x0007edb0)  Line 777 + 0x14 bytes C++
  // more Win32 libraries functions follow
  </code></pre>
  <p>I have lots of code and no idea where the buffer overrun might occur and why. I found <a href="http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/29243438-a5ef-404c-8b02-81e039a03459" rel="nofollow noreferrer">this forum discussion</a> and specifically the advise to replace all wcscpy-like functions with more secure versions like <a href="http://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx" rel="nofollow noreferrer">wcscpy_s()</a>. I followed the advise and that didn't get me closer to the problem solution.</p>
  <p>How do I debug my code and find why and where the buffer overrun occurs with Visual Studio 2008?</p>