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

前端 未结 11 1579
执笔经年
执笔经年 2020-12-15 16:54

Here is the error

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

相关标签:
11条回答
  • 2020-12-15 17:20

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-15 17:26

    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.

    0 讨论(0)
  • 2020-12-15 17:26

    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.

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

    0 讨论(0)
  • 2020-12-15 17:26

    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 讨论(0)
  • 2020-12-15 17:28

    If your solution configuration of Visual Studio is Release, switch to Debug.

    0 讨论(0)
  • 2020-12-15 17:28

    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.

    0 讨论(0)
提交回复
热议问题