简体   繁体   中英

How do I discover the return value at the end of a function when debugging in VS2008?

Using C# in Visual Studio 2008 and stepping through a function in the debugger I get to the end of a function and am on the final curly brace } and about to return. Is there a way to find out what value the function is about to return?

This is necessary if the return value is calculated such as:

return (x.Func() > y.Func());

It's a little low level, but if you switch to disassembly then you can single step through the instructions and see what the return value is being set to. It is typically set in the @eax register.

You can place a breakpoint on the ret instructions and inspect the register at that point if you don't want to single step through it.

I made a commercial extension for Visual Studio called BugAid (currently in beta) that does exactly what you asked.

In your example, it will show you this:

声明可视化

The way it does it is by instrumenting your code as you debug it, allowing us to retrieve the return values of both calls to "Func", and thus conclude whether the expression x.Func() > y.Func() is true or not.

For more information, please see my blog post on the subject .

You can put

(x.Func() > y.Func())

in a watch window to evaluate it, and see the result. Unless the statement is

return ValueChangesAfterEveryCall();

you should be fine.

I am still using VS 2003 with C++, so this may or may not apply. If you use the "auto" tab (near the "locals" and "watch" tabs), it will tell you the return value of a function once you return.

I'd actually recommend refactoring the code to put the individual function returns in local variables. That way, yourself and others don't have to jump through hoops when debugging the code to figure out a particular evaluation. Generally, this produces code that is easier to debug and, consequently, easier for others to understand and maintain.

int sumOfSomething = x.Func();
int pendingSomethings = y.Func();
return (sumOfSomething > pendingSomethings);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM