title: C++ exception "The thread tried to read from or write to a virtual address for which it does not have the appropriate access."
body: <p>In an effort to track down some hard to reproduce crashes I've configured the <code>UnhandledExceptionFilter</code> to create mini-dump files as described here: <a href="http://www.debuginfo.com/articles/debugfilters.html" rel="noreferrer">Debugging Custom Filters For Unhandled Exceptions</a> and <a href="http://www.debuginfo.com/articles/effminidumps2.html" rel="noreferrer">Effective Minidumps</a></p>
  
  <p>The dumps are being successfully captured but I'm not having much luck interpreting the stack information. In the hope that someone else will have experienced something similar I'll provide as many details as I can below.  Sorry if this question ends up being a little verbose as a result.</p>
  
  <p>Visual Studio provides the following dump summary:</p>
  
  <pre><code>Dump Summary
  ------------
  Dump File:  MiniDump.dmp
  Last Write Time:    15/08/2012 22:07:22
  Process Name:   Server.exe : C:\Project\Server.exe
  Process Architecture:   x86
  Exception Code: 0xC0000005
  Exception Information:  The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
  Heap Information:   Not Present
  
  System Information
  ------------------
  OS Version: 6.1.7601
  CLR Version(s): 
  
  Modules
  -------
  Module Name Module Path Module Version
  ----------- ----------- --------------
  Server.exe  C:\Project\Server.exe   1.0.0.1
  ntdll.dll   C:\Windows\SysWOW64\ntdll.dll   6.1.7601.17725
  kernel32.dll    C:\Windows\SysWOW64\kernel32.dll    6.1.7601.17651
  KERNELBASE.dll  C:\Windows\SysWOW64\KERNELBASE.dll  6.1.7601.17651
  mysqlpp_d.dll   C:\Projects\Ken11\bin\debug\mysqlpp_d.dll   0.0.0.0
  wsock32.dll C:\Windows\System32\wsock32.dll 6.1.7600.16385
  ws2_32.dll  C:\Windows\SysWOW64\ws2_32.dll  6.1.7601.17514
  msvcrt.dll  C:\Windows\SysWOW64\msvcrt.dll  7.0.7601.17744
  libmySQL.dll    C:\Projects\Ken11\bin\debug\libmySQL.dll    0.0.0.0
  user32.dll  C:\Windows\SysWOW64\user32.dll  6.1.7601.17514
  advapi32.dll    C:\Windows\SysWOW64\advapi32.dll    6.1.7601.17514
  msvcp90d.dll    C:\Projects\Ken11\bin\debug\Microsoft.VC90.DebugCRT\msvcp90d.dll    9.0.21022.8
  msvcr90d.dll    C:\Projects\Ken11\bin\debug\Microsoft.VC90.DebugCRT\msvcr90d.dll    9.0.21022.8
  mfc90d.dll  C:\Projects\Ken11\bin\debug\Microsoft.VC90.DebugMFC\mfc90d.dll  9.0.21022.8
  dbghelp.dll C:\Windows\System32\dbghelp.dll 6.1.7601.17514
  mswsock.dll C:\Windows\System32\mswsock.dll 6.1.7601.17514
  wininet.dll C:\Windows\SysWOW64\wininet.dll 8.0.7601.17785
  rasman.dll  C:\Windows\System32\rasman.dll  6.1.7600.16385
  devobj.dll  C:\Windows\SysWOW64\devobj.dll  6.1.7601.17621
  </code></pre>
  
  <p>Looking at the stack of the thread where the exception occurred we get the following extremely short call stack:</p>
  
  <pre><code>    msvcr90d.dll!6d69f824()     
      [Frames below may be incorrect and/or missing, no symbols loaded for msvcr90d.dll]  
  --&gt; Server.exe!CServer::LoadPageList()  Line 269 + 0x28 bytes   C++
      cccccccc()  
  </code></pre>
  
  <p>All the correct threads in the application have nice clean stacks showing calls all the way back to <code>__RtlUserThreadStart</code> E.g:</p>
  
  <pre><code>mswsock.dll!_WSPRecv@36()  + 0x34ff bytes   
  ws2_32.dll!_WSARecv@28()  + 0x71 bytes  
  wsock32.dll!_recv@16()  + 0x33 bytes    
  &gt;libmySQL.dll!008aaa2f()    
  [Frames below may be incorrect and/or missing, no symbols loaded for libmySQL.dll]  
  libmySQL.dll!008aac0d()     
  libmySQL.dll!008a34d7()     
  libmySQL.dll!008a30d2()     
  libmySQL.dll!0084be35()     
  libmySQL.dll!00851572()     
  libmySQL.dll!00851c2b()     
  Server.exe!CServer::logStatusToDB(AsynchStatus_T * pStatus=0x04f18170)  Line 1717   C++
  Server.exe!CServer::checkStatus(AsynchStatus_T * pStatus=0x04f18170)  Line 2145 C++
  Server.exe!CThread::ControllingFunction(void * lpParameter=0x03fffe08)  Line 1280 + 0xf bytes   C++
  kernel32.dll!@BaseThreadInitThunk@12()  + 0x12 bytes    
  ntdll.dll!___RtlUserThreadStart@8()  + 0x27 bytes   
  ntdll.dll!__RtlUserThreadStart@8()  + 0x1b bytes    
  </code></pre>
  
  <p>How could the lower portion of the crashed thread's stack get replaced by 0xCCCCCCCC?</p>
  
  <p>For completeness here is the disassembly of the frame <code>Server.exe!CServer::LoadPageList()  Line 269 + 0x28 bytes</code> </p>
  
  <pre><code>00453217  mov         byte ptr [ebp-4],0  
  0045321B  mov         esi,esp  
  0045321D  lea         ecx,[ebp-104h]  
  00453223  call        dword ptr [__imp_ATL::CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt; &gt; &gt;::~CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt; &gt; &gt; (6DA994h)]  
  00453229  cmp         esi,esp  
  0045322B  call        _RTC_CheckEsp (690D20h)  
  00453230  mov         dword ptr [ebp-4],0FFFFFFFFh  
  00453237  mov         esi,esp  
  00453239  lea         ecx,[ebp-20h]  
  0045323C  call        dword ptr [__imp_ATL::CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt; &gt; &gt;::~CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt; &gt; &gt; (6DA994h)]  
  00453242  cmp         esi,esp  
  00453244  call        _RTC_CheckEsp (690D20h)  
  00453249  push        edx  
  0045324A  mov         ecx,ebp  
  0045324C  push        eax  
  0045324D  lea         edx,[ (45327Ch)]  
  00453253  call        _RTC_CheckStackVars (690D50h)  
  00453258  pop         eax             &lt;&lt;&lt;=== exception refers to this line
  00453259  pop         edx  
  0045325A  mov         ecx,dword ptr [ebp-0Ch]  
  0045325D  mov         dword ptr fs:[0],ecx  
  00453264  pop         ecx  
  00453265  pop         edi  
  </code></pre>
  
  <p><em>Update</em></p>
  
  <p>Completely forgot to include the function source (thanks Joachim):</p>
  
  <pre><code>void CServer::LoadPageList()
  {
      CString header;
      header.Format("Accept: text/plain, */*; q=0.01\r\n");
      _pSession-&gt;setHttpHeader (header);
      _pSession-&gt;ReadPage (_T("/Pages"), true);
      SaveTimeStampedFile ("Pages.txt");
  }
  </code></pre>
  
  <p>The strange thing is that all of the operations in the function LoadPageList complete successfully, the crash seems to happen after the string's destructor is called just before returning from this stack frame.</p>