StackOverflowException after returning private variable, only while stepping through code

0

I'm getting a StackOverflowException in seemingly innocent code:

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
    ....
}

I can step through the code lines #1 - #4 OK. However, if I step into line #5 and wait half a second, the process terminates:

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).

(EDIT) A second way to reproduce the same symptom is to hover over _selectedModel with the mouse while debugging.

Note that the actual call stack never has more than 15-20 lines. I confirmed this behaviour in the following scenarios:

  • Debug / Release
  • Clean solution & Rebuild all

I find that if I remove all breakpoints and do NOT step through code, then execution continues without any problem.

Clearly this issue will hinder our debugging efforts and it may be obscuring a more serious problem.

What could be causing this?

c#
debugging
breakpoints
stack-overflow
asked on Stack Overflow Aug 7, 2014 by Brendan Hill • edited Aug 7, 2014 by Brendan Hill

1 Answer

4

Your TModel class is crashing the debugger evaluation thread. A simple example:

using System;

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

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

Step over the new statement and either hover over "obj" variable with the mouse or have the Locals or Autos debugger window visible to get the debugger to use the borken ToString() method. Fix your ToString() override or a debugger visualizer if you have one.

answered on Stack Overflow Aug 7, 2014 by Hans Passant

User contributions licensed under CC BY-SA 3.0