2

編輯:我發現了這個問題。調試器在沒有任何解釋的情況下殺死應用程序(在CryptoStream.ReadByte()調用中)

我的對象的構造函數正在以書寫模式初始化文件流,無論我們是否要加密或解密文件。即使寫入文件流在解密之前會被關閉(用於此目的的另一個文件流),它仍然會導致無聲的崩潰。現在它工作正常。

所以它看起來像一個相當普通的文件訪問問題。 CryptoStream沒有把它作爲例外處理,這真是一件好事。

感謝您的建議 - 我會看看ProcDumpWinDbg


我剛剛創建了一個小應用程序來讀取和解密本地硬盤上的文件。

Visual Studio調試器完全沒有任何消息或解釋而終止應用程序。在下面的代碼中執行read = crypto.ReadByte()行時發生。

  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<byte>(); 
       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; 
      } 

我嘗試和排除:

  1. 沒有異常拋出。我們從不輸入finallycatch。我也啓用了Break when an exception is thrown,但它沒有區別。沒有任何異常的跡象。

  2. 發佈應用程序(ClickOnce)並運行它具有相同的效果。

  3. 新鮮重新啓動沒有區別。

  4. 製作文件的副本(如果其他進程阻止訪問它等),並嘗試讀取該副本以相同的方式失敗。

  5. 如果我嘗試在監視窗口,以評估crypto.ReadByte(),我在爲了得到以下信息:

    • This expression causes side effects and will not be evaluated;

    • ,當我刷新:Function evaluation was aborted

    • 當我要麼刷新或只是等待幾秒鐘:Unable to evaluate the expression. The object invoked has disconnected from its clients.

和應用程序被終止,我們又回到了視覺工作室。

這裏有什麼問題?

+0

是'Key'初始化?看起來它在某些方面可能不正確。 –

+0

@Jeremy'Key'是硬編碼的。這是一個由Pi的8個第一個數字組成的字節數組。 –

+0

使用此密鑰進行編碼/解碼是否正常工作?嘗試一個專門的單元測試 - 至少這會消除這個原因。 –

回答

4

幾點建議:

  • 嘗試使用ProcDump與您的應用程序的-t選項。這應該會在您的應用程序過早退出時創建轉儲文件。嘗試其他選項(例如-e),如果它不產生有用的轉儲。
  • 如果這不起作用,請嘗試在WinDbg下運行您的應用程序。
0

我知道這麼晚回答,但也許它有助於一些。我有同樣的問題,這個答案可以幫助我Debugger kills the application with no explanation (on a CryptoStream.ReadByte() call)

如果您的觀察窗口包含錯誤消息「功能評估被中止。」這可能意味着顯示的屬性中的一個具有無限遞歸,就像這樣:

readonly bool isSigned; 
public bool IsSigned { get { return IsSigned; } } 

修復無限遞歸,問題就會消失:

readonly bool isSigned; 
public bool IsSigned { get { return isSigned; } } 

可以趕上StackOverflowException時,它拋出通過從菜單欄中選擇「Debug」 - >「Exceptions」 - >「Find ...」 - >鍵入「stackoverflow」 - >檢查System.StackOverflowException - >「OK」的「Thrown」,使用Visual Studio

相關問題