此應用程序具有由WinForms應用程序調用的WCF Web服務。 WinForms應用程序有一個類WCFCache
來管理服務中的數據。存在這樣類這樣的部分,其管理一個可選自定義配置部,該機器的一個子集具有:調試器中的AccessViolationException僅包含託管代碼和WCF服務
private bool? m_HasCustomConfiguration = null;
public bool HasCustomConfiguration
{
get
{
if (m_HasCustomConfiguration == null)
m_HasCustomConfiguration = (CustomConfiguration != null);
return (bool)m_HasCustomConfiguration;
}
}
private WCFService.CustomConfiguration m_CustomConfiguration = null;
public WCFService.CustomConfiguration CustomConfiguration
{
get
{
if (m_CustomConfiguration == null)
{
if (m_HasCustomConfiguration.HasValue
&& !m_HasCustomConfiguration.Value)
return null;
try
{
using (WCFService.WCFServiceClient wcf = new WCFService.WCFServiceClient())
{
m_CustomConfiguration =
wcf.GetCustomConfiguration(Machine.ProcessID);
// Above method returns null if no record exists.
m_HasCustomConfiguration = (m_CustomConfiguration != null);
}
} catch (Exception e) {
// Error logging & re-throw
}
}
return m_CustomConfiguration;
}
}
當我通過在代碼調試器調用任一這樣上述特性的步驟:
if (!Program.WCFCache.HasCustomConfiguration)
return new List<CustomComponents>();
...它引發以下例外:
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"
...
當我步驟到含線噸他提到,有一個很長的停頓,然後是除了彈出式窗口之外的例外。
上面的代碼執行完後,我只是放置一個斷點時,不會出現異常。當我在屬性的訪問器中放置斷點時,它甚至不會出現。只有當我踏上與外部其中一個屬性的界限時纔會發生。 (所以有一個解決方法,但它可能是一個痛苦。)
這是爲什麼發生?我可以阻止它嗎?
編輯:整個應用程序是在去年編寫的3.5 C#/ .NET與組件之間WCF通信;這意味着,我們沒有傳統的非託管DLL。只有兩個非託管調用:在加載WinForms應用程序時,在用戶名檢測過程中,一個用於advapi32.dll。我遇到的問題只發生在代碼中的這一個地方,在與登錄部分無關的地方,你可以得到。另一個是在kernel32.dll中,在一個GC force-flush long 之後,之後的任何事情都是從上面的調用結果中完成的。
在一個完全不相關的說明中,「HasCustomConfiguration」中的邏輯是否倒置?成員的名字表明它在'CustomConfiguration!= null'時應該是'true',但邏輯是反向的。 – 2011-03-03 19:46:50
正確。這是我的轉錄中的一個錯字。 :)修正。 – 2011-03-03 19:54:31
可能不是崩潰的原因,但您的HasCustomConfiguration的getter沒有返回值。 – 2011-03-03 21:10:45