Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible

Viewed 30072

Here is the error

Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.

I am writing a simple console app and the first line of code is this:

List<MyObjectModel> list = MyObjectModel.GetNonCompletedReturns();

and the code for the function is:

public static List<MyObjectModel> GetNonCompletedReturns()
{
    MyObject service = new MyObject();
    List<MyObject> entities = 
                      (from recs in service.Retrieve() where select recs).ToList();

    List<MyObjectModel> models = new List<MyObjectModel>();

    foreach (MyObject entity in entities)
    {
        models.Add(BindModel(entity));
    }

    return models;
}

and if I try to step through the code, as soon as I get back to the main of my app and hover over the list, I get the error message that I showed.

Can anyone help?

11 Answers

None of the answers solved my problem so I'm posting the solution that helped me.

"If there is to much data in the parameters then this error can occure, a simple solution is to make an object, not a struct because that's a dataobject.

Put this object in your parameters instead of all the different variables, normally the problem will no longer take place."

First make sure that you're running your code in DEBUG mode and with code optimization turned off. you can turn that off from the properties of your project.

If you made all of the above and the problem persists, then it's probably a problem with the stack having Debug.Break() on top of it. The solution for this is very easy, just press F10 to move to the next line and you should be able to evaluate the expression.

You can check this SO question for more information about this issue.

I was having same issue in Visual Studio 2017. Going to Debug> Options> Debugging> General and checking "Suppress JIT optimization on module load(Managed only)" fixed my issue

Related