Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) MemcheckExample2

 

STLQt CreatorLubuntu

 

memcheck example 2: a small memory leak is a memcheck example that shows how memcheck reports a memory leak we put in on purpose.

 

 

 

 

 

 

valgrind_memcheck.txt

 

Running the script valgrind_memcheck.sh resulted in the following output file:

 

==13939== Memcheck, a memory error detector
==13939== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==13939== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==13939== Command: ../CppMemcheckExample2-build-desktop/./CppMemcheckExample2
==13939== Parent PID: 13938
==13939==
--13939--
--13939-- Valgrind options:
--13939--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--13939--    --leak-check=full
--13939--    -v
--13939--    --show-reachable=yes
--13939--    --log-file=valgrind_memcheck.txt
--13939-- Contents of /proc/version:
--13939--   Linux version 2.6.38-8-generic-pae (buildd@vernadsky) (gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu3) ) #42-Ubuntu SMP Mon Apr 11 05:17:09 UTC 2011
--13939-- Arch and hwcaps: X86, x86-sse1-sse2
--13939-- Page sizes: currently 4096, max supported 4096
--13939-- Valgrind library directory: /usr/lib/valgrind
--13939-- Reading syms from /lib/i386-linux-gnu/ld-2.13.so (0x4000000)
--13939--   Considering /lib/i386-linux-gnu/ld-2.13.so ..
--13939--   .. CRC mismatch (computed 2a2c2799 wanted 1e351f1f)
--13939--   Considering /usr/lib/debug/lib/i386-linux-gnu/ld-2.13.so ..
--13939--   .. CRC is valid
--13939-- Reading syms from /home/richel/Projects/Website/CppMemcheckExample2-build-desktop/CppMemcheckExample2 (0x8048000)
--13939-- Reading syms from /usr/lib/valgrind/memcheck-x86-linux (0x38000000)
--13939--    object doesn't have a dynamic symbol table
--13939-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--13939-- Reading suppressions file: /usr/lib/valgrind/default.supp
--13939-- REDIR: 0x4016a80 (index) redirected to 0x3803f82b (vgPlain_x86_linux_REDIR_FOR_index)
--13939-- Reading syms from /usr/lib/valgrind/vgpreload_core-x86-linux.so (0x4020000)
--13939-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so (0x4023000)
==13939== WARNING: new redirection conflicts with existing -- ignoring it
--13939--     new: 0x04016a80 (index               ) R-> 0x04026c78 index
--13939-- REDIR: 0x4016c20 (strlen) redirected to 0x4027048 (strlen)
--13939-- Reading syms from /usr/lib/libQtCore.so.4.7.2 (0x4043000)
--13939--   Considering /usr/lib/libQtCore.so.4.7.2 ..
--13939--   .. CRC mismatch (computed 0c8a8d9d wanted ce78deb8)
--13939--   Considering /usr/lib/debug/usr/lib/libQtCore.so.4.7.2 ..
--13939--   .. CRC is valid
--13939-- Reading syms from /lib/i386-linux-gnu/libpthread-2.13.so (0x42d6000)
--13939--   Considering /lib/i386-linux-gnu/libpthread-2.13.so ..
--13939--   .. CRC mismatch (computed 87269ed5 wanted 7df95f2f)
--13939--   Considering /usr/lib/debug/lib/i386-linux-gnu/libpthread-2.13.so ..
--13939--   .. CRC is valid
--13939-- Reading syms from /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14 (0x42ef000)
--13939--    object doesn't have a symbol table
--13939-- Reading syms from /lib/i386-linux-gnu/libm-2.13.so (0x43da000)
--13939--   Considering /lib/i386-linux-gnu/libm-2.13.so ..
--13939--   .. CRC mismatch (computed e686ed93 wanted a0f1e52c)
--13939--   Considering /usr/lib/debug/lib/i386-linux-gnu/libm-2.13.so ..
--13939--   .. CRC is valid
--13939-- Reading syms from /lib/i386-linux-gnu/libgcc_s.so.1 (0x4400000)
--13939--   Considering /lib/i386-linux-gnu/libgcc_s.so.1 ..
--13939--   .. CRC mismatch (computed 3fa6d24f wanted 8b2bf89f)
--13939--    object doesn't have a symbol table
--13939-- Reading syms from /lib/i386-linux-gnu/libc-2.13.so (0x441c000)
--13939--   Considering /lib/i386-linux-gnu/libc-2.13.so ..
--13939--   .. CRC mismatch (computed 97e88cd2 wanted 4635a554)
--13939--   Considering /usr/lib/debug/lib/i386-linux-gnu/libc-2.13.so ..
--13939--   .. CRC is valid
--13939-- Reading syms from /lib/i386-linux-gnu/libz.so.1.2.3.4 (0x457e000)
--13939--   Considering /lib/i386-linux-gnu/libz.so.1.2.3.4 ..
--13939--   .. CRC mismatch (computed 995eb12f wanted 33a06a21)
--13939--    object doesn't have a symbol table
--13939-- Reading syms from /lib/i386-linux-gnu/libdl-2.13.so (0x4593000)
--13939--   Considering /lib/i386-linux-gnu/libdl-2.13.so ..
--13939--   .. CRC mismatch (computed 608d0daf wanted 983d6578)
--13939--   Considering /usr/lib/debug/lib/i386-linux-gnu/libdl-2.13.so ..
--13939--   .. CRC is valid
--13939-- Reading syms from /usr/lib/i386-linux-gnu/libgthread-2.0.so.0.2800.6 (0x4597000)
--13939--   Considering /usr/lib/i386-linux-gnu/libgthread-2.0.so.0.2800.6 ..
--13939--   .. CRC mismatch (computed 244482a3 wanted 61a9cd3c)
--13939--    object doesn't have a symbol table
--13939-- Reading syms from /lib/i386-linux-gnu/librt-2.13.so (0x459c000)
--13939--   Considering /lib/i386-linux-gnu/librt-2.13.so ..
--13939--   .. CRC mismatch (computed 680ec6ea wanted 247d4d27)
--13939--   Considering /usr/lib/debug/lib/i386-linux-gnu/librt-2.13.so ..
--13939--   .. CRC is valid
--13939-- Reading syms from /lib/i386-linux-gnu/libglib-2.0.so.0.2800.6 (0x45a5000)
--13939--   Considering /lib/i386-linux-gnu/libglib-2.0.so.0.2800.6 ..
--13939--   .. CRC mismatch (computed 2312631e wanted e21a44de)
--13939--    object doesn't have a symbol table
--13939-- Reading syms from /lib/i386-linux-gnu/libpcre.so.3.12.1 (0x467d000)
--13939--   Considering /lib/i386-linux-gnu/libpcre.so.3.12.1 ..
--13939--   .. CRC mismatch (computed 9e5ab3c1 wanted 29aaf7e3)
--13939--    object doesn't have a symbol table
--13939-- REDIR: 0x448ffb0 (strncmp) redirected to 0x4020479 (_vgnU_ifunc_wrapper)
--13939-- REDIR: 0x4497140 (strstr) redirected to 0x4020479 (_vgnU_ifunc_wrapper)
--13939-- REDIR: 0x4496db0 (__GI_strstr) redirected to 0x4028ef1 (strstr)
--13939-- REDIR: 0x44900b0 (rindex) redirected to 0x4026acc (rindex)
--13939-- REDIR: 0x439b650 (operator new(unsigned int)) redirected to 0x4026398 (operator new(unsigned int))
--13939-- REDIR: 0x448bef0 (malloc) redirected to 0x40267df (malloc)
--13939-- REDIR: 0x43994b0 (operator delete(void*)) redirected to 0x4025882 (operator delete(void*))
--13939-- REDIR: 0x448c3b0 (free) redirected to 0x4025b6b (free)
==13939==
==13939== HEAP SUMMARY:
==13939==     in use at exit: 4 bytes in 1 blocks
==13939==   total heap usage: 9 allocs, 8 frees, 340 bytes allocated
==13939==
==13939== Searching for pointers to 1 not-freed blocks
==13939== Checked 150,724 bytes
==13939==
==13939== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==13939==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==13939==    by 0x8048544: main (in /home/richel/Projects/Website/CppMemcheckExample2-build-desktop/CppMemcheckExample2)
==13939==
==13939== LEAK SUMMARY:
==13939==    definitely lost: 4 bytes in 1 blocks
==13939==    indirectly lost: 0 bytes in 0 blocks
==13939==      possibly lost: 0 bytes in 0 blocks
==13939==    still reachable: 0 bytes in 0 blocks
==13939==         suppressed: 0 bytes in 0 blocks
==13939==
==13939== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 33 from 6)
--13939--
--13939-- used_suppression:     33 U1004-ARM-_dl_relocate_object
==13939==
==13939== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 33 from 6)

 

valgrind did detect the memory leak in main, but does not report exactly which line caused this.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppMemcheckExample2/CppMemcheckExample2.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

SOURCES += main.cpp

 

 

 

 

 

./CppMemcheckExample2/main.cpp

 

int main()
{
  {
    int * p = new int;
    //Leak memory here
  }
}

 

 

 

 

 

./CppMemcheckExample2/valgrind_memcheck.sh

 

#!/bin/sh
valgrind --leak-check=full -v --show-reachable=yes --log-file=valgrind_memcheck.txt ../CppValgrindExample2-build-desktop/./CppValgrindExample2

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml