2012-02-29 113 views
4

我用我的程序在C++/CLI編碼與.NET框架4.有時LIB崩潰的外部C庫,我得到的消息:如何處理System.AccessViolationException?

未處理的異常:System.AccessViolationException:嘗試讀或寫保護的內存。

於是,我試着用try/catch塊來處理它:

try 
{ 
    Init(); //< lib call which sometimes crashes 
} 
catch (Exception^ e) 
{ 
    // handle the error 
} 

但例外仍然未捕獲的:我的程序崩潰進入catch塊之前。

如何處理此異常以防止我的程序崩潰?

+1

http://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception – Flot2011 2012-02-29 14:05:07

+0

點上!謝謝。 – gregseth 2012-02-29 14:45:52

回答

3

爲清楚起見我複製/由Flot2011在這裏的意見給出糊the answer

有幾種方法來解決這個問題:

  • 重新編譯爲.NET 3.5組裝和運行它在.NET 4.0中。
  • 添加一行到應用程序的配置文件中的配置/運行元素:<legacyCorruptedStateExceptionsPolicy enabled="true|false"/>
  • 裝飾你想與HandleProcessCorruptedStateExceptions屬性來捕獲這些異常的方法。詳情請參閱this article