views:

4749

answers:

8

I wrote some code with a lot of recursion, that takes quite a bit of time to complete. Whenever I "pause" the run to look at what's going on I get:

Cannot evaluate expression because the code of the current method is optimized.

I think I understand what that means. However, what puzzles me is that after I hit step, the code is not "optimized" anymore, and I can look at my variables. How does this happen? How can the code flip back and forth between optimized and non-optimzed code?

+2  A: 

You are probably trying to debug your app in release mode instead of debug mode, or you have optimizations turned on in your compile settings.

When the code is compiled with optimizations, certain variables are thrown away once they are no longer used in the function, which is why you are getting that message. In debug mode with optimizations disabled, you shouldn't get that error.

Lamar
No, I'm not trying to debug an app that was built in release mode.
Esteban Araya
"or you have optimizations turned on in your compile settings" <- also this isn't the case?
Biri
Correct, no optimizations either.
Esteban Araya
+4  A: 

This Link describes what / why you may be seeing this behavior.

Nescio
I've wondered about this myself, it would be nice to summarize the salient points of the blog post in your answer.
Joe
This link is really interesting.
Lamar
Have this blog posted pointed a solution or it just explained the problem?
Jader Dias
This is not an answer. Please do not just link (especially to large blocks of info that do not clearly answer the question).
Catskul
A: 

I believe that what you are seeing is a result of the optimisations - sometimes a variable will be reused - particularly those that are created on the stack. For example, suppose you have a method that uses two (local) integers. The first integer is declared at the start of the method, and is used solely as a counter for a loop. Your second integer is used after the loop has been completed, and it stores the result of a calculation that is later written out to file. In this case, the optimiser MAY decide to reuse your first integer, saving the code needed for the second integer. When you try to look at the second integer early on, you get the message that you are asking about "Cannot evaluate expression". Though I cannot explain the exact circumstances, it is possible for the optimiser to transfer the value of the second integer into a separate stack item later on, resulting in you then being able to access the value from the debugger.

Anthony K
+9  A: 

While the Debug.Break line is on top of the classstack you cant eval expressions. That's beacuse that line is optimized. Press F10 to move to the next line - a valid line of code - and the watch will work.

No one
This is the answer. Hopefully it will eventually get marked as such.
Catskul
Yes, I hope so too.
Nescio
A: 

Friend of a friend from Microsoft sent this: 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 getting optimized because your method signature is too large.

A: 

Had the same problem but was able to resolve it by turning off exception trapping in the debugger. Click [Debug][Exceptions] and set the exceptions to "User-unhandled".

Normally I have this off but it comes in handy occasionally. I just need to remember to turn it off when I'm done.

+1  A: 

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

A: 

This drove me crazy. I tried attaching with Managed and Native code - no go.

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

  • Go into Project / Properties
  • Select the Build tab and click Advanced...
  • Make sure Debug Info is set to "full" (not pdb-only)
  • Debug your project - voila!
Ralph177