Professional C__ - Marc Gregoire [471]
void ArticleCitations::readFile(const string& fileName)
{
// Code omitted for brevity
mCitations = nullptr;
mNumCitations = 0;
if (count != 0) {
// Allocate an array of strings to store the citations.
mCitations = new string[count];
mNumCitations = count;
// Clear the previous eof.
istr.clear();
// Seek back to the start of the citations.
istr.seekg(citationsStart);
// Read each citation and store it in the new array.
for (count = 0; count < mNumCitations; count++) {
string temp;
getline(istr, temp);
mCitations[count] = temp;
}
}
}
Code snippet from ArticleCitations\FinalVersion\ArticleCitations.cpp
As this example shows, memory errors don’t always show up right away. It often takes a debugger and some persistence to figure them out.
If you attempt to replicate this debugging session on a different platform, you may find that, due to the vagaries of memory errors, the program crashes in a different place than this example shows.
Using the Code::Blocks Debugger
The previous section gives a demonstration of how you can use the popular gdb debugger on Linux. This is a command-line debugger. Code::Blocks on the other hand is a free open-source graphical IDE (Integrated Development Environment), which includes a debugger. Basically, Code::Blocks is a graphical user interface on top of the GCC compiler and the gdb debugger, and is available for several different platforms. This section demonstrates how you can use this graphical debugger to debug the same ArticleCitations example from the previous section.
This example assumes that Code::Blocks is installed properly on your system. Consult the Code::Blocks website for instructions. The first thing you have to do is start Code::Blocks and create a new project. Select Console Application as your new project type and click on the Go button. You will be asked if you want to use C or C++; choose C++ and continue. On the next page of the wizard you need to specify a name for the new project. Choose “ArticleCitations” and select the folder where you want Code::Blocks to save your new project. On the last page of the wizard you need to select your C++ compiler. Select GNU GCC Compiler and click the Finish button. Code::Blocks will now create your new project.
Once the project has been created, you will see a Management tree on the left. This tree contains your source code files. Initially it contains a file called main.cpp. Delete that file from the list. Then right-click the ArticleCitations project in the Management tree and choose Add Files. Add ArticleCitations.cpp and ArticleCitations.h from the DebuggerDebugging folder in the downloadable code archive, and add ArticleCitationsTest.cpp. Figure 27-1 shows how the Management tree should look after this step.
FIGURE 27-1
Now you can compile the project by choosing Build from the Build menu. If everything was set up correctly, the compilation should succeed without errors.
To test the application you first need to copy paper1.txt and paper2.txt to the ArticleCitations project folder. The two text files should be in the same folder as the ArticleCitations.cbp Code::Blocks project file. To start debugging the program, click on Debug and then Start. First, type paper1.txt and press enter. The paper1 citations will be shown. Next, type paper2.txt and press enter. The application crashes and Code::Blocks will automatically break into the debugger. To find out where the crash happened, go to the Debug menu, click on Debugging Windows and then on Call Stack. This will show you the call stack at the time of the crash. This call stack might look like Figure 27-2.
FIGURE 27-2
If you run this on a different platform, your call stack might look different due to the vagaries of memory errors.
The first frame that is in your code is frame #1, the ArticleCitations copy constructor. When you double-click that frame, Code::Blocks will jump to it, and will mark the current