Home > Debugging, Visual studio > Function return values live in VC watch window!

Function return values live in VC watch window!

Ever felt the need to watch over return values from functions! Well I did always but unfortunately couldn’t come up with something! I was reading through one of John Robbins’ Bugslayer articles in MSDN and found an interesting tip related to seeing function parameters passed in by adding a watch statement over the ESP stack pointer variable.

So that got me thinking like “why not EAX”, huh that was easy! So this tip happens due to the tip given by one of the readers of John’s article in Bugslayer. I will put a link to that tip once I find it along with the author’s name.

Now let me tell you how the watch statement for our tip should look like…

  1. To watch string values returned by functions…
    (char*)EAX
  2. To watch integer values returned by functions…
    (int*)EAX
  3. To watch objects returned by functions…
    (CMyObject*)EAX

Now you may ask why do I need this, I can always use the auto variable debug window. Well don’t forget that we are adding watch to a register (EAX), so this means even raw function call’s return value is shown here.

Let me show you a live case where you may need this. See here I’ve got four function calls which does some strange operations but return values doensn’t matter hence they are not stored. It’s just useless to store return values in temporary var just for the purpose of debugging, so above watches come handy.

DoOperationReturnBool(); // Returns bool, but returned values not stored
DoOperationReturnEnum(); // Cast EAX to appropriate enum type or int
DoOperationReturnCharPtr(); // Cast EAX to char pointer  or wide char ptr
DoOperationReturnObject(); // Cast EAX to appropriate object type, I haven’t tested this but should work

I am using this these days, yet to see a drawback, tell me if you find any! Have fun debugging 🙂

Advertisements
Categories: Debugging, Visual studio Tags: , ,
  1. November 15, 2008 at 11:03 am

    Thanks Steve!

    Addresses are always displayed in hex, so as you did it should be cast to int.

  2. Steve
    November 14, 2008 at 7:25 pm

    Rock on! Thanks for posting this.

    Also, the Value for my (int*)EAX was showing up as hex even though Hexadecimal Display was off. To work around that, I used (int)((int*)EAX)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: