-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_check.txt
More file actions
104 lines (93 loc) · 4.57 KB
/
memory_check.txt
File metadata and controls
104 lines (93 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Checking for memory leaks or memory errors. on linux is done with a tool
called "valgrind". This tool will identify memory leaks and errors and can
help you find their location. The basic way to run valgrind is as follows:
valgrind --leak-check=yes ./myprogram
Substitute "myprogram" for your program. The result will look something like
this:
==5535== Memcheck, a memory error detector
==5535== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==5535== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==5535== Command: ./a.out
==5535==
==5535==
==5535== HEAP SUMMARY:
==5535== in use at exit: 72,704 bytes in 1 blocks
==5535== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==5535==
==5535== LEAK SUMMARY:
==5535== definitely lost: 0 bytes in 0 blocks
==5535== indirectly lost: 0 bytes in 0 blocks
==5535== possibly lost: 0 bytes in 0 blocks
==5535== still reachable: 72,704 bytes in 1 blocks
==5535== suppressed: 0 bytes in 0 blocks
==5535== Reachable blocks (those to which a pointer was found) are not shown.
==5535== To see them, rerun with: --leak-check=full --show-reachable=yes
==5535==
==5535== For counts of detected and suppressed errors, rerun with: -v
==5535== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Notice the "LEAK SUMMARY" section. In this case is shows no leaks. The
following example shows a memory leak.
==5551== Memcheck, a memory error detector
==5551== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==5551== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==5551== Command: ./a.out
==5551==
==5551==
==5551== HEAP SUMMARY:
==5551== in use at exit: 72,705 bytes in 2 blocks
==5551== total heap usage: 2 allocs, 0 frees, 72,705 bytes allocated
==5551==
==5551== 1 bytes in 1 blocks are definitely lost in loss record 1 of 2
==5551== at 0x4C27D49: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==5551== by 0x4008D9: main (t.cpp:9)
==5551==
==5551== LEAK SUMMARY:
==5551== definitely lost: 1 bytes in 1 blocks
==5551== indirectly lost: 0 bytes in 0 blocks
==5551== possibly lost: 0 bytes in 0 blocks
==5551== still reachable: 72,704 bytes in 1 blocks
==5551== suppressed: 0 bytes in 0 blocks
==5551== Reachable blocks (those to which a pointer was found) are not shown.
==5551== To see them, rerun with: --leak-check=full --show-reachable=yes
==5551==
==5551== For counts of detected and suppressed errors, rerun with: -v
==5551== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Notice that the "LEAK SUMMARY" tells me that there is a leaked byte of
memory. Also, the "HEAP SUMMARY" section tells me that that byte was allocated
on line 9 of the t.cpp file. In order to get the detailed location information
you need to compiler your program using g++ with the "-g" option.
Sometimes valgrind will report no leaks but there will be memory errors. For
example, the following output from valgrind shows no memory leaks but it does
show that the program is using unitialized values.
==10931== Memcheck, a memory error detector
==10931== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==10931== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==10931== Command: ./err
==10931==
hello
==10931== Conditional jump or move depends on uninitialised value(s)
==10931== at 0x4C2ACE9: strlen (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10931== by 0x4F47258: std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&, char const*) (char_traits.h:267)
==10931== by 0x40088C: main (in /home/inst/mike.noel/tmp/err)
==10931==
==10931==
==10931== HEAP SUMMARY:
==10931== in use at exit: 72,704 bytes in 1 blocks
==10931== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==10931==
==10931== LEAK SUMMARY:
==10931== definitely lost: 0 bytes in 0 blocks
==10931== indirectly lost: 0 bytes in 0 blocks
==10931== possibly lost: 0 bytes in 0 blocks
==10931== still reachable: 72,704 bytes in 1 blocks
==10931== suppressed: 0 bytes in 0 blocks
==10931== Rerun with --leak-check=full to see details of leaked memory
==10931==
==10931== For counts of detected and suppressed errors, rerun with: -v
==10931== Use --track-origins=yes to see where uninitialised values come from
==10931== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
These memory errors also need to be addressed.
Using valgrind you can find and eliminate your memory leaks.