Debugging C++ Method Parameters

New Rule: Make sure your parameter names are consistent between your declarations and definitions in C++.

Here is why…

I was trying to debug some C++/CLR code today when I ran into an interesting behavior (bug?) in Visual Studio 2005. I had a breakpoint in the first line of my method which I hit fine. I then tried to inspect the value of the two integers that were passed in. The first one showed up fine when I moused over it, but the second one didn’t appear. Strange, but I’ve come to expect that debugging in C++.

The next thing I tried was the Watch window (Ctrl+Alt+W,1). I added both of the parameters. The first showed up fine, but the second said “error: identifier ‘maxRows’ out of scope.” How could it be out of scope, I was on the first line in the method?

Next I looked at the Locals window (Ctrl+Alt+V,L). The parameter was showing up fine there! Then I noticed the difference, the name of the parameter in the locals window wasn’t pluralized. Sure enough, I looked in the header file and it was spelled maxRow there.

It turns out that the debug symbols are taken from the header files, so when you step into a method with parameters that are spelled differently, then you must use the values in the header file to inspect the variables.

Here is an example I mocked up where the parameters were x and y in the header files.


I also tried this in unmanaged C++ and got the same result.

2 thoughts on “Debugging C++ Method Parameters

  1. It seems that the bug in the link you provided is slightly different. It is for local variables, not method parameters. Also the Microsoft KB article it points to is not the same issue, that is for C# and is problems evaluating things like typeof.

    As for the wrong line of code, yes I’ve seen that since the early betas of VS 2005. It is really annoying, but you get used to it. The worst is that the C++ debugger always steps into the first line of an if statement whether or not it is false. If the if was false, it then immediately steps out.

    If you have a single line if statement, you can never tell if it really went in or not without inspecting the variables. Very annoying and it has caused some erroneous debug sessions on my part a few times.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>