开发者

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

开发者 https://www.devze.com 2023-03-12 17:55 出处:网络
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.

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 sele开发者_高级运维ct 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?


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


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.


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


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.


While it's true that the "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized" error appears when in release mode, most developers just ensure that their projects are configured to compile as a debug build. BUT to be sure that you have no release-DLL issues, you also must check the references to DLLs that are in your solution and make sure that you don't have a reference to a release-build DLL. If you find that this is the case, delete the DLL reference and then add a project reference rather than a DLL reference. The project reference will ensure that your solution references debug or release versions of the DLL as specified in your build configuration.

Note that the above advice applies, of course, to only those DLLs to which you have source code and which are built from a project in your solution.


I got this too, when I hit a NullReferenceException from a 3rd party control.

In this one case, I found that if I set a breakpoint before I hit the exception, I could then single step through the rest of the code without seeing the problem.

No idea why, but this worked for me - in this case at least.


For what it's worth, this error can also be caused by an infinite loop in a property getter (simplified version below). When the debugger attempts to evaluate the property (e.g. in the watch window) the UI will hang for a few seconds and the "Cannot evaluate expression..." error will appear for many of the other properties in the same class.

public int MyProperty
{
    get
    {
        while (true) { }
        return 0;
    }
}


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 experiencing the same error message in the Visual Studio debugger when evaluating a linq expression.

Disabling the VS debugger config setting 'Enable Just My Code' resolved the issue for me:

To enable or disable Just My Code, choose the Tools > Options menu in Visual Studio. In the Debugging > General node, choose or clear Enable Just My Code.

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

https://learn.microsoft.com/en-us/visualstudio/debugger/just-my-code


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

0

精彩评论

暂无评论...
验证码 换一张
取 消