(Q) Exercising valgrind

Note

Valgrind works in Linux desktop and in Linux computers, but most probably not in Mac nor Windows (or installing valgrind in these computers is impossible).

However, with Mac you can use valgrind via ssh as follows: In Mac command line, write the command:

ssh user_id@linux-desktop.tuni.fi

where user_id shall be replaced with your own user id. If you are asked ”Are you sure you want to continue …”, you can answer ”yes”. After that continue as told below at Executing ``valgrind`` on the command line.

Goal: I will learn to use valgrind for tracking the problems with memory management, and to interpret the output of valgrind.

This is important because, in the remaining projects, one requirement is that the submitted program has no problems with memory management. You are supposed to make sure that there are none by using valgrind during the test phase.

Instructions: Retrieve the code template: templates/09/valgrind/ -> student/09/valgrind/.

Let us examine this small program. You can see several problems to do with memory management right away:

#include <iostream>

using namespace std;

int main() {
    int number1;
    int number2 = 111;
    int *ptr1 = new int;
    int *ptr2 = new int(222);

    cout << number1 << " "
         << number2 << " "
         << *ptr1 << " "
         << *ptr2 << endl;

    delete ptr1;

    *ptr1 = 333;
}

The next two subsections give instructions to use valgrind both in Qt Creator and in command line. If possible, you can try both of these environments and compare them with each other.

Executing valgrind in Qt Creator

In Qt Creator, open the project you moved into the directory student/09/valgrind/.

If you want, you can execute the program. In the Application Output window, there should be the output ”exited with code 0”, that is, the execution of the program finished with the return value EXIT_SUCCESS, which means that everything went well (despite the program containing basically nothing but errors). Please note that when a program has as many problems with memory management as does our example program, it is not guaranteed that it will execute without hiccups. When the personnel created the assignment, it was possible to execute the program all the way to the end with the compiler installed on linux-desktop, but we cannot guarantee that it works similarly in all environments.

Execute valgrind by selecting Analyze > Valgrind Memory Analyzer. The program starts, but this time, the Application Output window will contain the output ”Analyzing finished”.

Also, Qt Creator opens the window Memcheck, and in its black top bar, it is supposed to say ”Memory Analyzer Tool finished, 8 issues were found”. We will examine the contents of that window in this assignment. If your Qt Creator shows nothing below the titles Issue and Location, click the filter icon located in the black top bar of the window and put a check on ”External Errors”. This will make the 8 problems valgrind found appear on the list.

There are two columns in the Memcheck window of Qt Creator: Issue and Location. There are two ways to access the Location column after clicking a downwards arrow to open one of the long lines and have a look at its contents:

  • by scrolling the bar at the bottom of the Memcheck window to the right.
  • by moving the cursor over the line where the Location column of interest to you is, and waiting until you see a box containing Location, in case the chosen line has data in the Location column.

Executing valgrind on the command line

Start the command line user interface and navigate into the directory where you saved the abovementioned program code (the project directory of the previous section).

  1. Compile your source code manually by writing the compilation command into the Linux command line:

    g++ -std=c++11 -Wall -g main.cpp
    

    If your source code contained no errors, the result will be the executable program (a file in machine language) called a.out.

  2. If you want, you can try executing the program by writing this command on the Linux command line:

    ./a.out
    
  3. Then execute valgrind command for the compiled program:

    valgrind --quiet --leak-check=full ./a.out
    

    If there are no memory problems in that program, there will be no extra outputs on your screen aside from the outputs of your program.

    Because this example program did contain problems with memory management, valgrind prints an enormous amount of information, and we will now take a good look at it. You need to see the output from the very beginning, so scroll the terminal upwards far enough for you to see the valgrind command you wrote. (Tip: you also could stretch the terminal window as long as you can for easier working.)

At the beginning of each command line output, there is writing like this ”==6647==” and, on some lines, something like this ”at 0x4F31BE3:” as well. Qt Creator does not show any of these, which lets you deduce that they are irrelevant information and you can read the command line output without addressing them at all.

Using an uninitialized variable

The error messages of valgrind are not the clearest there are, so after seeing them print different things for different problems in this simple program snippet, you will be able to understand them better.

Therefore, examine the code and the error messages it has caused, and find out what kind of an error message valgrind gives if you use a uninitialized variable.

Not deallocating memory

What is the error message valgrind gives you for not deallocating memory?

Using memory you already deallocated

What is the error message valgrind gives you for using memory you already deallocated?

Executing valgrind command

Static analysis means that you analyze program code without executing it. Dynamic analysis, however, means that you analyze the working of a program in a certain execution situation (with certain inputs, for example). The dynamic analysis only finds the errors that the execution in question reveals.

What can you deduce from the fact that Qt Creator starts up the execution of a program in the process window every time you execute the valgrind analysis to a program?

Command line switches

Again, execute valgrind from the command line. This time, your command should be:

valgrind --verbose --leak-check=full ./a.out

i.e. instead of the switch --quiet, you will use the switch --verbose. How does that affect your print?

Using the command line, continuation

The programming professional interviewed in section 13.1 told us: ”Some options of the build-tools you need in serious development are easily accessible only via the command line. Graphical integrations are usually not developed as fast as I would need them, if at all.”

What does this mean in practice? For example, on the matter of analyzing errors of memory management, it means that when valgrind was published, it was only used on the command line. Qt Creator’s valgrind connection was implemented at a later stage.

The companies often want to use the newest tools, which means it is important for a professional to know how to use the command line.

Finally, try executing Analyze > Valgrind Memory Analyzer with GDB in Qt Creator. You will see that it starts up valgrind in debugging mode, which allows you to step through the program code line by line, like debuggers usually do. Now, the window Application Output has a print that looks the same as the one in the terminal window.

When quitting, please make sure you have closed the debugger!

Usually, Qt Creator goes from Edit mode to Debug mode when you execute valgrind. Sometimes, Qt Creator does not do so, and to see the Memcheck window, you need to go from Edit mode to Debug mode yourself. When you encounter this ”characteristic” (?), it is good to understand that valgrind is always executed in Debug mode in Qt Creator.