title: C# error "Attempted to read or write protected memory" or "External component has thrown an exception"
body: <p>I have a C# program (target framework .NET 4.0) which makes calls to COM objects (unmanaged code), all objects managed properly, destroyed when no longer required, etc.</p>
  
  <p>I have also few exceptions handler, try/catch blocks without custom exceptions. However some times this program crashed, works most of the times, random behaviour. Stack traces also vary, example: </p>
  
  <pre><code>System.AccessViolationException was unhandled
    Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    Source=System.Windows.Forms
    StackTrace:
         at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.Run(Form mainForm)
         at IndexerTester.Program.Main() in D:\Tester\Program.cs:line 17
    InnerException:
  </code></pre>
  
  <p>In Program.cs, Line 17: Application.Run(new Form1());</p>
  
  <pre><code>System.Runtime.InteropServices.SEHException was unhandled
    Message=External component has thrown an exception.
    Source=System.Windows.Forms
    ErrorCode=-2147467259
    StackTrace:
         at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.Run(Form mainForm)
         at IndexerTester.Program.Main() in D:\Tester\Program.cs:line 17
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException:
  </code></pre>
  
  <p>I am in isolation stage to find out root of this error. </p>
  
  <ul>
  <li>Do you have any suggestions of this error?</li>
  <li>Any suggestions about catch these exceptions and exit gracefully.</li>
  </ul>
  
  <p>Thanks for your help.</p>