(Q) Exercising valgrind¶
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/10/valgrind/
->
student/10/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;
}
In this assignment, you will compare using valgrind
between
Qt Creator and command line.
Executing valgrind
in Qt Creator¶
In Qt Creator, open the project you moved into the directory
student/10/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.
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).
Compile your source code manually by writing the compilation command into the Linux command line:
g++ -std=c++11 -Wall -g <cpp files>
If your source code contained no errors, the result will be the executable program (a file in machine language) called
a.out
.If you want, you can try executing the program by writing this command on the Linux command line:
./a.out
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 thevalgrind
command you wrote. (Tip: you also could stretch the terminal window as long as you can for easier working.)
Set Qt Creator and the terminal window side by side on your screen so that you can see both of them. This will make your work easier.
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! (We practiced this at the end of assignment 2.2.2.)
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.