Professional C__ - Marc Gregoire [472]
FIGURE 27-3
From this, you notice that the following assignment is causing the problem:
mCitations[i] = src.mCitations[i];
At the bottom of the Code::Blocks window there should be a Debugger tab that looks like Figure 27-4.
FIGURE 27-4
In this Debugger tab you can type gdb commands like in the previous section. To debug the crash in the copy constructor, you can inspect the src parameter by typing the following in the Debugger tab:
print src
From the output you will see that mNumCitations is five. However, paper2 doesn’t have any citations so mNumCitations should be zero. The reason and the fix is exactly the same as in the previous section.
Using the Visual C++ 2010 Debugger
This section explains the same debugging procedure as described in the previous section, but uses the Microsoft Visual C++ 2010 debugger instead of Code::Blocks.
First, you need to create a project. Start VC++ and click on File ⇒ New ⇒ Project. In the project template tree on the left, select Visual C++ ⇒ Win32. Then select the Win32 Console Application template in the list in the middle of the window. At the bottom you can give a name for the project and a location where to save it. Specify ArticleCitations as the name, choose a folder where to save the project, and click the OK button. A wizard will open. Click the Next button, select Empty Project, and click Finish.
Once your new project is loaded, you can see a list of project files in the Solution Explorer. If this docking window is not visible, go to View ⇒ Solution Explorer. Right click the ArticleCitations project in the Solution Explorer and click Add ⇒ Existing Item. Add all the files from the ArticleCitations\VisualStudio folder in the downloadable code archive to the project. Your Solution Explorer should look similar to Figure 27-5.
FIGURE 27-5
Now you can compile the program; click on Build ⇒ Build Solution.
Copy the paper1.txt and paper2.txt test files to your ArticleCitations project folder, which is the folder containing the ArticleCitations.vcxproj file.
Run the application with Debug ⇒ Start Debugging, and test the program by first specifying the paper1.txt file. It should properly read the file and output the result to the console. Then, type paper2.txt, which causes the VC++ debugger to break the execution. You will get a message saying “Unhandled exception” in which you need to click the Break button.
At this point, you should inspect the call stack, Debug ⇒ Windows ⇒ Call Stack. In this call stack, you need to find the first line that contains code that you wrote. This is shown in Figure 27-6.
FIGURE 27-6
Just as with gdb and Code::Blocks, you see that the problem is in the ArticleCitations copy constructor. You can double click that line in the call stack window to jump to the right place in the code. Then click on Debug ⇒ Windows ⇒ Autos to inspect variables. In the list of variables you can find the src parameter. Click on the plus sign to expand the data members of the src variable. Figure 27-7 shows how it looks.
FIGURE 27-7
From this window, you see that src.mNumCitations is five, while it should be zero. The reason and the fix is exactly the same as earlier.
Lessons from the ArticleCitations Example
You might be inclined to disregard this example as too small to be representative of real debugging. Although the buggy code is not lengthy, many classes that you write will not be much bigger, even in large projects. Thus, this example corroborates the message from Chapter 26 about the importance of unit testing. Imagine if you had failed to test this example thoroughly before integrating it with the rest of the project. If these bugs showed up later, you and other engineers would have to spend more time narrowing down the problem before you could debug it as shown here. Additionally, the techniques shown in this example apply to all debugging, large scale or small.
SUMMARY
The most important concept in this chapter was the Fundamental Law of Debugging: Avoid bugs when