Debugging C++ Method Parameters

2007, Sep 11    

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.

CppDebugging

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