一般錯誤的發生通常是在程式執行下,很容易在Debug-Mode或用Try Catch抓出,但這個Exception發生的時機與一般不同,並非出現在某行指令執行,因此光只是在事件or方法中用Try Catch包死死就可以抓出皮漏,至少在我發生的時機,完全無法取得錯誤資訊,害我被這個可惡的Bug攪和了一整天,好不容易才找到原因。
稍為描述我遇到這個Exception,由於是在WinCE環境下發生,當下沒有抓圖軟體可以用,完整的錯誤如下:

Fatal Application Error
Application TD_Control.exe has performed an illegal operation and will be shut down.
If the problem persists, contact the program vendor.

Program: TDS_Control.exe
Exception: 0xC0000005
Address: 01B84871

0xC0000005是一個「違反記憶體使用權」的錯誤,像矩陣存取超界到不允許的記憶體空間,就算是「違反記憶體使用權」,你一定會說:「矩陣超界會有其他錯誤訊息!」沒錯,一般情況下如此,但也有例外,在MSDN論壇MVP Alex回答,未初始化的常數做Index時,也常會發生此類的錯誤訊息,他回答如下:

This error (access violation) is most frequently caused by a unitialized value. E.g. if you have

int i;

int arrValues[10];

...

int z = arrValues[i];

and you never set the value of i, in Debug mode it will be initialized to 0xCDCDCDCD and naturally cause an access violation when used as an array index. In release mode it willl be 0 and not cause any errors, but produce a wrong result.

To catch this, run the application under debugger and in the Debug/Exceptions open Win32 Exceptions and check the box against c0000005, then run the application and see the stack when it breaks.

Alex講的很清楚,i變數未給定初值,在Debug Mode會被給定0xCDCDCDCD,此時執行arrValues[i]便會出現0xC0000005。可是我相信真只是初始值那樣簡單的問題,相信大部分程式設計師也不會搞那麼久,檢查每個變數是否給定初值應該不用花1分鐘,但此也不失是一個好方向,發生「非使用權之記憶體」也有可能是arrVaules消失,也就是物件被回收卻被函數呼叫或參考。

而我的狀況是,使用System.Threading.Timer的Callback會傳遞另一個物件作Status,但在呼叫Dispose時卻先把會被傳遞的物件給Dispose了,當此時不巧Timer一執行,便會發生此等錯誤。



arrow
arrow
    全站熱搜

    傑克鼠 發表在 痞客邦 留言(2) 人氣()