Using Valgrind to detect Memory Leaks (still reachable)

ABOUT Memory Leak

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations[1] in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.[2] A memory leak has symptoms similar to a number of other problems and generally can only be diagnosed by a programmer with access to the program's source code.

A space leak occurs when a computer program uses more memory than necessary. In contrast to memory leaks, where the leaked memory is never released, the memory consumed by a space leak is released, but later than expected. [3]

Because they can exhaust available system memory as an application runs, memory leaks are often the cause of or a contributing factor to software aging.

ABOUT Valgrind

Valgrind /ˈvælɡrɪnd/ is a programming tool for memory debugging, memory leak detection, and profiling.

Valgrind was originally designed to be a free memory debugging tool for Linux on x86, but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers.

The name Valgrind is a reference to the main entrance of Valhalla from Norse Mythology. During development (before release) the project was named Heimdall; however, the name would have conflicted with a security package.

TYPICAL COMMANDLINE SESSION
[text light=”true”]
$valgrind -v –tool=memcheck –leak-check=full ./ethstatus -i eth1
==19477== Memcheck, a memory error detector
==19477== Copyright (C) 2002-2012, and GNU GPL’d, by Julian Seward et al.
==19477== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==19477== Command: ./ethstatus -i eth1
==19477==
–19477– Valgrind options:
–19477– –suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
–19477– -v
–19477– –tool=memcheck
–19477– –leak-check=full
–19477– Contents of /proc/version:
–19477– Linux version 3.11.0 (root@debian) (gcc version 4.7.3 (Debian 4.7.3-4) ) #1 SMP Mon Sep 16 00:32:04 IST 2013
–19477– Arch and hwcaps: AMD64, amd64-sse3-cx16-lzcnt-avx
–19477– Page sizes: currently 4096, max supported 4096
–19477– Valgrind library directory: /usr/lib/valgrind
–19477– Reading syms from /home/jeffrin/Cloud/debian-develop/ethstatus-0.4.3/ethstatus
–19477– Reading syms from /lib/x86_64-linux-gnu/ld-2.17.so
–19477– Considering /lib/x86_64-linux-gnu/ld-2.17.so ..
–19477– .. CRC mismatch (computed bf253f8a wanted e079394a)
–19477– Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.17.so ..
–19477– .. CRC is valid
–19477– Reading syms from /usr/lib/valgrind/memcheck-amd64-linux
–19477– Considering /usr/lib/valgrind/memcheck-amd64-linux ..
–19477– .. CRC mismatch (computed 3468463e wanted c047c437)
–19477– Considering /usr/lib/debug/usr/lib/valgrind/memcheck-amd64-linux ..
–19477– .. CRC is valid
–19477– object doesn’t have a dynamic symbol table
–19477– Scheduler: using generic scheduler lock implementation.
–19477– Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
–19477– Reading suppressions file: /usr/lib/valgrind/default.supp
==19477== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-19477-by-jeffrin-on-???
==19477== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-19477-by-jeffrin-on-???
==19477== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-19477-by-jeffrin-on-???
==19477==
==19477== TO CONTROL THIS PROCESS USING vgdb (which you probably
==19477== don’t want to do, unless you know exactly what you’re doing,
==19477== or are doing some strange experiment):
==19477== /usr/lib/valgrind/../../bin/vgdb –pid=19477 …command…
==19477==
==19477== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==19477== /path/to/gdb ./ethstatus
==19477== and then give GDB the following command
==19477== target remote | /usr/lib/valgrind/../../bin/vgdb –pid=19477
==19477== –pid is optional if only one valgrind process is running
==19477==
–19477– REDIR: 0x4017890 (strlen) redirected to 0x3806c661 (vgPlain_amd64_linux_REDIR_FOR_strlen)
–19477– Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so
–19477– Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
–19477– .. CRC mismatch (computed 3468909c wanted 2ffaf283)
–19477– Considering /usr/lib/debug/usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
–19477– .. CRC is valid
–19477– Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
–19477– Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
–19477– .. CRC mismatch (computed c6ae4a33 wanted 21397e4b)
–19477– Considering /usr/lib/debug/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
–19477– .. CRC is valid
–19477– REDIR: 0x4017700 (index) redirected to 0x4c2c4b0 (index)
–19477– REDIR: 0x4017780 (strcmp) redirected to 0x4c2d4c0 (strcmp)
–19477– Reading syms from /usr/lib/debug/libncurses.so.5.9
–19477– Reading syms from /usr/lib/debug/libtinfo.so.5.9
–19477– Reading syms from /lib/x86_64-linux-gnu/libc-2.17.so
–19477– Considering /lib/x86_64-linux-gnu/libc-2.17.so ..
–19477– .. CRC mismatch (computed b0b3185d wanted a268c1003)
–19477– Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.17.so ..
–19477– .. CRC is valid
–19477– Reading syms from /lib/x86_64-linux-gnu/libdl-2.17.so
–19477– Considering /lib/x86_64-linux-gnu/libdl-2.17.so ..
–19477– .. CRC mismatch (computed 2a2457cb wanted a5918d1a)
–19477– Considering /usr/lib/debug/lib/x86_64-linux-gnu/libdl-2.17.so ..
–19477– .. CRC is valid
–19477– REDIR: 0x5317e40 (strcasecmp) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x5314200 (strnlen) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x531a110 (strncasecmp) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x5316c50 (memset) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x5316c00 (memcpy@GLIBC_2.2.5) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x5315bf0 (__GI_strrchr) redirected to 0x4c2c2d0 (__GI_strrchr)
–19477– REDIR: 0xffffffffff600400 (???) redirected to 0x3806c64d (vgPlain_amd64_linux_REDIR_FOR_vtime)
–19477– REDIR: 0x5314120 (__GI_strlen) redirected to 0x4c2c830 (__GI_strlen)
–19477– REDIR: 0x530e4f0 (free) redirected to 0x4c2aa70 (free)
–19477– REDIR: 0x530e0d0 (malloc) redirected to 0x4c2bd80 (malloc)
–19477– REDIR: 0x5313ab0 (__GI_strcpy) redirected to 0x4c2c920 (__GI_strcpy)
–19477– REDIR: 0x5312620 (__GI_strcmp) redirected to 0x4c2d470 (__GI_strcmp)
–19477– REDIR: 0x531dc10 (__GI___rawmemchr) redirected to 0x4c2ee90 (__GI___rawmemchr)
–19477– REDIR: 0x531de20 (strchrnul) redirected to 0x4c2ee40 (strchrnul)
–19477– REDIR: 0x5314230 (__GI_strnlen) redirected to 0x4c2c7e0 (__GI_strnlen)
–19477– REDIR: 0x5312560 (__GI_strchr) redirected to 0x4c2c3b0 (__GI_strchr)
Running EthStatus v0.4a…–19477– REDIR: 0x5314320 (__GI_strncmp) redirected to 0x4c2cd00 (__GI_strncmp)
–19477– REDIR: 0x53140d0 (strlen) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x530ea30 (calloc) redirected to 0x4c29ba0 (calloc)
–19477– REDIR: 0x5315bb0 (rindex) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x5312520 (index) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x5313a70 (strcpy) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x53125e0 (strcmp) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x531c7f0 (memcpy@@GLIBC_2.14) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x531c840 (__GI_memcpy) redirected to 0x4c2d7f0 (memcpy@@GLIBC_2.14)
–19477– REDIR: 0x530e580 (realloc) redirected to 0x4c2bf80 (realloc)
–19477– REDIR: 0x5315b70 (strncpy) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x53214b0 (__GI_strncpy) redirected to 0x4c2cb40 (__GI_strncpy)
–19477– REDIR: 0x532b4f0 (strstr) redirected to 0x4a24710 (_vgnU_ifunc_wrapper)
–19477– REDIR: 0x532b060 (__GI_strstr) redirected to 0x4c2f240 (strstr)

EthStatus v0.4a – Gabriel Montenegro / Christoph Haas

==19477==
==19477== HEAP SUMMARY:
==19477== in use at exit: 85,525 bytes in 122 blocks
==19477== total heap usage: 22,300 allocs, 22,178 frees, 6,549,861 bytes allocated
==19477==
==19477== Searching for pointers to 122 not-freed blocks
==19477== Checked 227,344 bytes
==19477==
==19477== LEAK SUMMARY:
==19477== definitely lost: 0 bytes in 0 blocks
==19477== indirectly lost: 0 bytes in 0 blocks
==19477== possibly lost: 0 bytes in 0 blocks
==19477== still reachable: 85,525 bytes in 122 blocks
==19477== suppressed: 0 bytes in 0 blocks
==19477== Reachable blocks (those to which a pointer was found) are not shown.
==19477== To see them, rerun with: –leak-check=full –show-reachable=yes
==19477==
==19477== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
–19477–
–19477– used_suppression: 2 dl-hack3-cond-1
==19477==
==19477== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
$

[/text]
LINKS
https://en.wikipedia.org/wiki/Valgrind
https://en.wikipedia.org/wiki/Memory_leak
https://stackoverflow.com/questions/26137356/how-to-check-actual-memory-leaks-during-runtime-using-valgrind
https://www.ibm.com/developerworks/library/l-memory-leaks/index.html
https://www.ibm.com/developerworks/systems/library/es-debug/index.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s