title: Debugger kills the application with no explanation (on a CryptoStream.ReadByte() call)
body: <p><strong>EDIT:</strong> I identified the problem.</p>
  
  <p>The constructor for my object was initializing a file stream in writing mode, whether or not we were about to encrypt or decrypt the file. Even though the writing file stream would be closed before decrypting (another file stream used for that purpose), it was still causing the silent crash. Now it works fine.</p>
  
  <p>So it looks like a rather ordinary file access issue. It's a pitty that <code>CryptoStream</code> does not handle it as an exception.</p>
  
  <p>Thanks for the suggestions - I'll have a look at <strong>ProcDump</strong> and <strong>WinDbg</strong>. </p>
  
  <hr>
  
  <p>I just created a small application to read and decrypt a file from local HDD.</p>
  
  <p>Visual Studio debugger kills the application without any message or explanation at all. It happens when the line <code>read = crypto.ReadByte()</code> in the following code is executed.</p>
  
  <pre><code>            try
              {
                  if (file != null)
                  {
                      // the stream that was used for encryption
                      file.Close();
                  }
                  var provider = new DESCryptoServiceProvider();
                  provider.Key = Key;
                  provider.IV = Key;
                  var ict = provider.CreateDecryptor();
                  var bytes = new List&lt;byte&gt;();
                  using (var stream = new FileStream(fileName, FileMode.Open))
                  {
                      crypto = new CryptoStream(stream, ict, CryptoStreamMode.Read);
                      var decrypted = new MemoryStream();
                      Int32 read;
                      try
                      {
                          do
                          {
                              read = crypto.ReadByte(); // the application dies here
                              if (read != -1) bytes.Add(Convert.ToByte(read));
                          }
                          while (read != -1);
                      }
                      finally
                      {
                          // we never get here
                          decrypted.Close();
                      }
  
                  };
                  return ASCIIEncoding.ASCII.GetString(bytes.ToArray());
              }
              catch (Exception x)
              {
                  // we never get here either                            
                  crypto.Close();
                  return x.Message;
              }
  </code></pre>
  
  <p><strong>What I tried and ruled out:</strong></p>
  
  <ol>
  <li><p>No exceptions are thrown. We never enter <code>finally</code> nor <code>catch</code>. I also enabled <code>Break when an exception is thrown</code>, but it makes no difference. No indication of any exception.</p></li>
  <li><p>Publishing the application (ClickOnce) and running it has the same effect.</p></li>
  <li><p>Fresh restart makes no difference.</p></li>
  <li><p>Making a copy of the file (in case if some other process was blocking access to it etc.) and trying to read that copy fails in the same manner.</p></li>
  <li><p>If I try to evaluate <code>crypto.ReadByte()</code> in the Watch window, I'm getting the following messages in that order:</p>
  
  <ul>
  <li><p><code>This expression causes side effects and will not be evaluated</code>;</p></li>
  <li><p>when I refresh it: <code>Function evaluation was aborted</code></p></li>
  <li><p>when I either refresh it or just wait a few seconds: <code>Unable to evaluate the expression. The object invoked has disconnected from its clients.</code></p></li>
  </ul></li>
  </ol>
  
  <p>and the app is terminated, we're back to Visual Studio.</p>
  
  <p>What is the problem here?</p>