簡體   English   中英

返回私有變量后的StackOverflowException,僅在單步執行代碼時

[英]StackOverflowException after returning private variable, only while stepping through code

我在看似純真的代碼中得到了StackOverflowException:

private void OnSelectedModelChanged(object sender, EventArgs eventArgs)
{
    LoadNoticeDetails();  // Line #1
}

private void DoNothing()
{
    // Never reaches here
}

private void LoadNoticeDetails()
{
    if (SelectedModel == null) return; // Line #2
    DoNothing(); Line #5
    ... // Never reaches here
}

private TModel _selectedModel;

public TModel SelectedModel
{
    get
    {
        return _selectedModel; // Line #3
    } // Line #4
    ....
}

我可以逐步執行#1-#4代碼行。 但是,如果我進入第5行並等待半秒鍾,則該過程終止:

Process is terminated due to StackOverflowException.
'blahblah.exe' (CLR v4.0.30319: blahblah.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. 
The program '[14936]  blahblah.exe' has exited with code -2147023895 (0x800703e9).

(編輯)重現相同症狀的第二種方法是在調試時將鼠標懸停在_selectedModel上。

請注意,實際的調用堆棧最多不會有15-20行。 我在以下情況下確認了此行為:

  • 調試/發布
  • 清潔解決方案並全部重建

我發現如果刪除所有斷點並且不單步執行代碼,那么執行將繼續進行而不會出現任何問題。

顯然,此問題將阻礙我們的調試工作,並且可能掩蓋了更嚴重的問題。

是什么原因造成的?

您的TModel類使調試器評估線程崩潰。 一個簡單的例子:

using System;

class Program {
    static void Main(string[] args) {
        var obj = new TModel();
    }
}

class TModel {
    public override string ToString() {
        return " " + ToString();
    }
}

跳過語句,然后用鼠標懸停在“ obj”變量上,或者使“本地或自動”調試器窗口可見,以使調試器使用borken ToString()方法。 修復ToString()覆蓋或調試器可視化器(如果有)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM