Which means "Unable to evaluate the expression because the code of the current method is optimized." I mean?

I wrote some code with a lot of recursion, which takes quite a while to complete. Whenever I “pause” the launch to see what happens, I get:

The expression cannot be evaluated because the code for the current method is optimized.

I think I understand what that means. However, what puzzles me is that after I pressed the step, the code is no longer optimized, and I can look at my variables. How did this happen? How can code flip between optimized and non-optimized code?

+44
optimization compiler-construction debugging c #
Sep 25 '08 at 5:40
source share
15 answers

The debugger uses FuncEval so you can "look at" the variables. FuncEval requires that threads be stopped in managed code at a secure GarbageCollector point. Manually pausing startup in the IDE causes all threads to stop as soon as possible. Your very recursive code will tend to stop at an unsafe point. Therefore, the debugger cannot evaluate expressions.

Pressing the F10 button will move to the next safe point Funceval and enable the evaluation function.

See the FuncEval rules for more information.

+26
Sep 25 '08 at 6:00
source share

While the Debug.Break () line is at the top of the call, you cannot express expressions. This is because this line is optimized. Press F10 to go to the next line - the actual line of code - and the watch will work.

+44
Dec 12 '08 at 9:46
source share

You are probably trying to debug your application in release mode instead of debug mode, or you have optimization in compilation settings.

When the code is compiled with optimization, some variables are thrown as soon as they are no longer used in the function, so you get this message. In debug mode with optimizations disabled, you should not get this error.

+25
Sep 25 '08 at 5:46
source share

It drove me crazy. I tried installing with managed and native code - no need.

This worked for me, and I was finally able to evaluate all the expressions:

  • Go to Project / Properties
  • Select the Assembly tab and click More ...
  • Make sure Debug Info is set to "full" (not just pdb)
  • Debug your project - voila!
+7
Aug 03 '10 at 15:43
source share

Below I worked for me, thanks @Vin.

I had this problem when I used VS 2015. My solution: the configuration has (debugging). I solved this by unchecking the Optimize Code property in the project properties.

Project (right click) => Properties => Build (tab) => clear the Optimize code check box

+4
Apr 26 '16 at 16:35
source share

Look for a function call with many parameters and try decreasing the number until debugging returns.

+2
Nov 14 '09 at 2:34
source share

Make sure you don’t have something like this

 [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] 

in AssemblyInfo

+2
Oct 06 '16 at 1:39 on
source share

A friend from Microsoft sent: http://blogs.msdn.com/rmbyers/archive/2008/08/16/Func_2D00_eval-can-fail-while-stopped-in-a-non_2D00_optimized-managed-method-that-pushes- more-than-256-argument-bytes-.aspx

The most likely problem is that your call stack is optimized because your method signature is too large.

+1
Mar 09 '09 at 22:11
source share

Had the same problem, but was able to resolve it by disabling the exception exception in the debugger. Click [Debug] [Exceptions] and set the exceptions for "Raw user".

I usually get this, but sometimes it comes in handy. I just have to remember to turn it off when done.

+1
Mar 10 '09 at 14:44
source share

I had this problem when I used VS 2010. My solution configuration was selected (Debug). I solved this by unchecking the "Optimize code" property in the project properties. Project (right click) => Properties => Build (tab) => clear the Optimize code check box

+1
May 2, '11 at 21:42
source share

In my case, I had 2 projects in my solution and was executing a project that was not a startup project. When I changed it to a launch project, debugging started working again.

Hope this helps someone.

0
Mar 13 '15 at 22:00
source share

Rating:

In .NET, “Function Evaluation (funceval)” is the ability of the CLR to make an arbitrary call, while debuggee stops somewhere. Funceval is responsible for the selected debugger stream to execute the requested method. When funceval ends, it fires a debug event. Technically, the CLR has defined debugging methods for the release of funceval.

The CLR allows you to initiate funceval only for those threads that are in the GC security point (that is, when the thread will not block the GC) and Funceval Safe (FESafe) points (i.e. where the CLR can actually make a capture for funceval.) together, Therefore, possible scenarios for the CLR, the stream should be:

  • stopped in managed code (and at the GC security point): This means that we cannot execute funceval in native code. Since native code is outside the CLR control, it cannot configure funceval.

  • stops at the 1st random or unhandled guided exception (and at the GC safe point): i.e. during the exception, check as much as possible to determine why this exception occurred. (for example: the debugger may try to evaluate and see the Message property in the raised exception.)

In general, common ways to stop in managed code include stopping at a breakpoint, step, calling Debugger.Break, catching an exception, or starting a thread. This helps in evaluating the method and expressions.

Possible permissions: Based on the assessment, if the stream is not at the FESafe and GCSafe points, the CLR will not be able to capture the stream to initiate a funceval. Typically, the following helps ensure that funceval initiates when expected:

Step 1:

Make sure you are not trying to debug the Release assembly. The release is fully optimized and, thus, will lead to an error in the discussion. Using the standard toolbar or Configuration Manager, you can switch between Debug and Release.

Step # 2:

If you still receive an error, the Debug option may be specified for optimization. Check and uncheck "Optimize code" in the "Properties" section:

Right-click on the project. Select the Properties option. Click the Build tab. Uncheck "Optimize code"

Step number 3:

If you still receive an error message, you may need the wrong Debug Info mode. Check and set it to "full" in the "Advanced build settings" section:

Right-click on the project. Select the Properties option. Click the Build tab. Click the Advanced button. Set Debug Info to Full.

Step # 4:

If you are still experiencing a problem, try the following:

Do "Clean" and then "Restore" the solution file. When debugging: Go to the modules window (VS Menu → Debug → Windows → Modules) Find your assembly in the list of loaded modules. Verify that the path specified against the loaded assembly is what you expect from it. Check the modified timestamp of the file to confirm that the assembly was actually rebuilt. Check whether the loaded module is optimized or not.

Output:

This is not an error, but information based on specific settings and developed based on how the .NET runtime works.

0
Oct 28 '15 at 11:43
source share

in my case, I was in release mode, which I changed to debug all of this.

0
Mar 20 '16 at 3:35
source share

I had a similar problem and it was solved when I built the solution in debug mode and replaced the pdb file in the execution path.

0
Nov 08 '17 at 9:07 on
source share

I believe that what you see is the result of optimizations - sometimes a variable will be reused, especially those created on the stack. For example, suppose you have a method that uses two (local) integers. The first integer is declared at the beginning of the method and is used exclusively as a counter for the loop. The second integer is used after the completion of the cycle and stores the result of the calculation, which is later written to the file. In this case, the optimizer MAY decide to reuse your first integer, keeping the code needed for the second integer. When you try to look at the second integer at an early stage, you will receive a message stating that you are asking "Unable to evaluate expression." Although I cannot explain the exact circumstances, the optimizer can later pass the value of the second integer to a separate element of the stack, as a result of which you can access the value from the debugger.

-one
Sep 25 '08 at 6:30
source share



All Articles