Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) MemcheckExample3

 

STLQt CreatorLubuntu

 

memcheck example 3: deleting an array the wrong way is a memcheck example 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:

 

==14146== Memcheck, a memory error detector
==14146== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==14146== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==14146== Command: ../CppMemcheckExample3-build-desktop/./CppMemcheckExample3
==14146== Parent PID: 14145
==14146==
--14146--
--14146-- Valgrind options:
--14146--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--14146--    --leak-check=full
--14146--    -v
--14146--    --show-reachable=yes
--14146--    --log-file=valgrind_memcheck.txt
--14146-- Contents of /proc/version:
--14146--   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
--14146-- Arch and hwcaps: X86, x86-sse1-sse2
--14146-- Page sizes: currently 4096, max supported 4096
--14146-- Valgrind library directory: /usr/lib/valgrind
--14146-- Reading syms from /lib/i386-linux-gnu/ld-2.13.so (0x4000000)
--14146--   Considering /lib/i386-linux-gnu/ld-2.13.so ..
--14146--   .. CRC mismatch (computed 2a2c2799 wanted 1e351f1f)
--14146--   Considering /usr/lib/debug/lib/i386-linux-gnu/ld-2.13.so ..
--14146--   .. CRC is valid
--14146-- Reading syms from /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3 (0x8048000)
--14146-- Reading syms from /usr/lib/valgrind/memcheck-x86-linux (0x38000000)
--14146--    object doesn't have a dynamic symbol table
--14146-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--14146-- Reading suppressions file: /usr/lib/valgrind/default.supp
--14146-- REDIR: 0x4016a80 (index) redirected to 0x3803f82b (vgPlain_x86_linux_REDIR_FOR_index)
--14146-- Reading syms from /usr/lib/valgrind/vgpreload_core-x86-linux.so (0x4020000)
--14146-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so (0x4023000)
==14146== WARNING: new redirection conflicts with existing -- ignoring it
--14146--     new: 0x04016a80 (index               ) R-> 0x04026c78 index
--14146-- REDIR: 0x4016c20 (strlen) redirected to 0x4027048 (strlen)
--14146-- Reading syms from /usr/lib/libQtCore.so.4.7.2 (0x4043000)
--14146--   Considering /usr/lib/libQtCore.so.4.7.2 ..
--14146--   .. CRC mismatch (computed 0c8a8d9d wanted ce78deb8)
--14146--   Considering /usr/lib/debug/usr/lib/libQtCore.so.4.7.2 ..
--14146--   .. CRC is valid
--14146-- Reading syms from /lib/i386-linux-gnu/libpthread-2.13.so (0x42d6000)
--14146--   Considering /lib/i386-linux-gnu/libpthread-2.13.so ..
--14146--   .. CRC mismatch (computed 87269ed5 wanted 7df95f2f)
--14146--   Considering /usr/lib/debug/lib/i386-linux-gnu/libpthread-2.13.so ..
--14146--   .. CRC is valid
--14146-- Reading syms from /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14 (0x42ef000)
--14146--    object doesn't have a symbol table
--14146-- Reading syms from /lib/i386-linux-gnu/libm-2.13.so (0x43da000)
--14146--   Considering /lib/i386-linux-gnu/libm-2.13.so ..
--14146--   .. CRC mismatch (computed e686ed93 wanted a0f1e52c)
--14146--   Considering /usr/lib/debug/lib/i386-linux-gnu/libm-2.13.so ..
--14146--   .. CRC is valid
--14146-- Reading syms from /lib/i386-linux-gnu/libgcc_s.so.1 (0x4400000)
--14146--   Considering /lib/i386-linux-gnu/libgcc_s.so.1 ..
--14146--   .. CRC mismatch (computed 3fa6d24f wanted 8b2bf89f)
--14146--    object doesn't have a symbol table
--14146-- Reading syms from /lib/i386-linux-gnu/libc-2.13.so (0x441c000)
--14146--   Considering /lib/i386-linux-gnu/libc-2.13.so ..
--14146--   .. CRC mismatch (computed 97e88cd2 wanted 4635a554)
--14146--   Considering /usr/lib/debug/lib/i386-linux-gnu/libc-2.13.so ..
--14146--   .. CRC is valid
--14146-- Reading syms from /lib/i386-linux-gnu/libz.so.1.2.3.4 (0x457e000)
--14146--   Considering /lib/i386-linux-gnu/libz.so.1.2.3.4 ..
--14146--   .. CRC mismatch (computed 995eb12f wanted 33a06a21)
--14146--    object doesn't have a symbol table
--14146-- Reading syms from /lib/i386-linux-gnu/libdl-2.13.so (0x4593000)
--14146--   Considering /lib/i386-linux-gnu/libdl-2.13.so ..
--14146--   .. CRC mismatch (computed 608d0daf wanted 983d6578)
--14146--   Considering /usr/lib/debug/lib/i386-linux-gnu/libdl-2.13.so ..
--14146--   .. CRC is valid
--14146-- Reading syms from /usr/lib/i386-linux-gnu/libgthread-2.0.so.0.2800.6 (0x4597000)
--14146--   Considering /usr/lib/i386-linux-gnu/libgthread-2.0.so.0.2800.6 ..
--14146--   .. CRC mismatch (computed 244482a3 wanted 61a9cd3c)
--14146--    object doesn't have a symbol table
--14146-- Reading syms from /lib/i386-linux-gnu/librt-2.13.so (0x459c000)
--14146--   Considering /lib/i386-linux-gnu/librt-2.13.so ..
--14146--   .. CRC mismatch (computed 680ec6ea wanted 247d4d27)
--14146--   Considering /usr/lib/debug/lib/i386-linux-gnu/librt-2.13.so ..
--14146--   .. CRC is valid
--14146-- Reading syms from /lib/i386-linux-gnu/libglib-2.0.so.0.2800.6 (0x45a5000)
--14146--   Considering /lib/i386-linux-gnu/libglib-2.0.so.0.2800.6 ..
--14146--   .. CRC mismatch (computed 2312631e wanted e21a44de)
--14146--    object doesn't have a symbol table
--14146-- Reading syms from /lib/i386-linux-gnu/libpcre.so.3.12.1 (0x467d000)
--14146--   Considering /lib/i386-linux-gnu/libpcre.so.3.12.1 ..
--14146--   .. CRC mismatch (computed 9e5ab3c1 wanted 29aaf7e3)
--14146--    object doesn't have a symbol table
--14146-- REDIR: 0x448ffb0 (strncmp) redirected to 0x4020479 (_vgnU_ifunc_wrapper)
--14146-- REDIR: 0x4497140 (strstr) redirected to 0x4020479 (_vgnU_ifunc_wrapper)
--14146-- REDIR: 0x4496db0 (__GI_strstr) redirected to 0x4028ef1 (strstr)
--14146-- REDIR: 0x44900b0 (rindex) redirected to 0x4026acc (rindex)
--14146-- REDIR: 0x439b650 (operator new(unsigned int)) redirected to 0x4026398 (operator new(unsigned int))
--14146-- REDIR: 0x448bef0 (malloc) redirected to 0x40267df (malloc)
--14146-- REDIR: 0x439b790 (operator new[](unsigned int)) redirected to 0x4025ece (operator new[](unsigned int))
--14146-- REDIR: 0x4399510 (operator delete[](void*)) redirected to 0x4025504 (operator delete[](void*))
--14146-- REDIR: 0x43994b0 (operator delete(void*)) redirected to 0x4025882 (operator delete(void*))
==14146== Mismatched free() / delete / delete []
==14146==    at 0x4025907: operator delete(void*) (vg_replace_malloc.c:387)
==14146==    by 0x80485D9: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485EF: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==  Address 0x46be360 is 0 bytes inside a block of size 40 alloc'd
==14146==    at 0x4025F53: operator new[](unsigned int) (vg_replace_malloc.c:299)
==14146==    by 0x80485D1: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485EF: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==
==14146== Mismatched free() / delete / delete []
==14146==    at 0x4025907: operator delete(void*) (vg_replace_malloc.c:387)
==14146==    by 0x80485D9: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485F9: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==  Address 0x46be410 is 0 bytes inside a block of size 40 alloc'd
==14146==    at 0x4025F53: operator new[](unsigned int) (vg_replace_malloc.c:299)
==14146==    by 0x80485D1: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485F9: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==
--14146-- REDIR: 0x448c3b0 (free) redirected to 0x4025b6b (free)
==14146==
==14146== HEAP SUMMARY:
==14146==     in use at exit: 0 bytes in 0 blocks
==14146==   total heap usage: 12 allocs, 12 frees, 496 bytes allocated
==14146==
==14146== All heap blocks were freed -- no leaks are possible
==14146==
==14146== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 33 from 6)
==14146==
==14146== 1 errors in context 1 of 2:
==14146== Mismatched free() / delete / delete []
==14146==    at 0x4025907: operator delete(void*) (vg_replace_malloc.c:387)
==14146==    by 0x80485D9: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485F9: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==  Address 0x46be410 is 0 bytes inside a block of size 40 alloc'd
==14146==    at 0x4025F53: operator new[](unsigned int) (vg_replace_malloc.c:299)
==14146==    by 0x80485D1: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485F9: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==
==14146==
==14146== 1 errors in context 2 of 2:
==14146== Mismatched free() / delete / delete []
==14146==    at 0x4025907: operator delete(void*) (vg_replace_malloc.c:387)
==14146==    by 0x80485D9: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485EF: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==  Address 0x46be360 is 0 bytes inside a block of size 40 alloc'd
==14146==    at 0x4025F53: operator new[](unsigned int) (vg_replace_malloc.c:299)
==14146==    by 0x80485D1: Bad() (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==    by 0x80485EF: main (in /home/richel/Projects/Website/CppMemcheckExample3-build-desktop/CppMemcheckExample3)
==14146==
--14146--
--14146-- used_suppression:     33 U1004-ARM-_dl_relocate_object
==14146==
==14146== ERROR SUMMARY: 2 errors from 2 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: ./CppMemcheckExample3/CppMemcheckExample3.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

 

 

 

 

 

./CppMemcheckExample3/main.cpp

 

void Good()
{
  int * p = new int[10];
  delete[] p;
}

void Bad()
{
  int * p = new int[10];
  delete p; //Bad: memory leak, should call delete[]
}

int main()
{
  Good();
  Bad();
  Good();
  Bad();
}

 

 

 

 

 

./CppMemcheckExample3/valgrind_memcheck.sh

 

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

 

 

 

 

 

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