Cannot evaluate expression because the current thread is in a stack overflow state

Topics: Prism v2 - Silverlight 2
May 3, 2009 at 7:05 PM

public

 

ExpenseListPresentationModel(IEventAggregator eventAggregator, IExpenseListView expenseListView)

 

{

this.View = expenseListView;

 


The underlined code causes the problem. If I inspect the Model property before the line executes it is null as expected but once it enters the setter it causes the stack overflow.

Model = Cannot evaluate expression because the current thread is in a stack overflow state.
May 3, 2009 at 7:11 PM
changing from 
public IExpenseListView View
{
 get
 {
  return this.View;
 }
 set
 {
  this.View = value;
 }
}

to 
public IExpenseListView View {get; set;}

avoided the stack overflow.

Can anyone explain please?
May 4, 2009 at 6:34 AM
I acutally had a similar issue and fixed it using a work around similar to yours :-).

I dont exactly know why this is happening, but when you have a view model refer to the view and the view refer to the view model, there is a cyclic reference and this could be causing the stack overflow exception.
Feb 6, 2010 at 7:29 PM

Okay, my guess:

We have:

public IExpenseListView View
{
 get
 {
  return this.View;
 }
 set
 {
  this.View = value;
 }
}

If you call the setter ( ExpenseListPresentationModel.View = IExpenseListView)  it will call ExpenseListPresentationModel.View = IExpenseListView, which will call ExpenseListPresentationModel.View = IExpenseListView. Thus: stack overflow.

Similar is true for calling the getter.

Your change replaced the setter/getter implementations (accessing a hidden, generated backing field instead of performing infinite recursion), thereby removing the stack overflow.