Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

linq

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?

like image 864
esastincy Avatar asked Jun 14 '11 18:06

esastincy


4 Answers

If your project is compiled in release (with optimizations turned on), you may see this. Have you tried the DEBUG configuration?

like image 114
agent-j Avatar answered Oct 16 '22 02:10

agent-j


This error fires only when you are trying to use Watch dialog during debug. Try to use some other technique to output the variables, like Debug.WriteLine, Console.WriteLine and so on.

like image 23
VMAtm Avatar answered Oct 16 '22 03:10

VMAtm


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

like image 2
Igor Meszaros Avatar answered Oct 16 '22 01:10

Igor Meszaros


Here's a little trick just in case you want to examine some objects and you are not able to change the parameters:

I've created a call to a new temporary function, inside the function from where I was unable to watch my object. Then, inside that new function I was able to watch my object. After the job is done, just delete the function.

like image 1
Roger Avatar answered Oct 16 '22 02:10

Roger