|
From: Guntram S. <gun...@ia...> - 2007-09-14 16:40:10
|
I have a problem using valgrind with tool memcheck analysing my program.=
Valgrind (3.2.3) produces a segmentation fault:
here is the simplified version of the code, which produces the same erro=
r if
N is large enough (N > 2048, on my machine with 1GB main memory / 1GB sw=
ap).
If N is small enough, valgrind detects no error.
It seems that valgrind needs N*(N+1)/2 times more memory space than the
program requires itself.
Is this my problem or valgrind's?
#include <stdlib.h>
#include <string.h>
#define SIZE 1024
#define N 4096
int main(int argc, char** argv) {
long int i =3D 0;
long int size =3D 0;
const char value[1024];
char* image =3D NULL;
for(i =3D 0; i < N; i++) {
image =3D (char*)realloc(image, (size + SIZE) * sizeof(char));
memcpy(image + size, value, SIZE * sizeof(char));
size +=3D SIZE;
}
if(image !=3D NULL) free(image);
return 0;
}
and that's what valgrind says:
~> valgrind -v --tool=3Dmemcheck --leak-check=3Dfull --show-reachable=3D=
yes test1
=3D=3D19536=3D=3D Memcheck, a memory error detector.
=3D=3D19536=3D=3D Copyright (C) 2002-2007, and GNU GPL'd, by Julian Sewa=
rd et al.
=3D=3D19536=3D=3D Using LibVEX rev 1732, a library for dynamic binary tr=
anslation.
=3D=3D19536=3D=3D Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks L=
LP.
=3D=3D19536=3D=3D Using valgrind-3.2.3, a dynamic binary instrumentation=
framework.
=3D=3D19536=3D=3D Copyright (C) 2000-2007, and GNU GPL'd, by Julian Sewa=
rd et al.
=3D=3D19536=3D=3D
--19536-- Command line
--19536-- test1
--19536-- Startup, with flags:
--19536-- -v
--19536-- --tool=3Dmemcheck
--19536-- --leak-check=3Dfull
--19536-- --show-reachable=3Dyes
--19536-- Contents of /proc/version:
--19536-- Linux version 2.6.18.8-0.5-default (geeko@buildhost) (gcc ve=
rsion 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Fri Jun 22 12:17:=
53 UTC 2007
--19536-- Arch and hwcaps: X86, x86-sse1-sse2
--19536-- Page sizes: currently 4096, max supported 4096
--19536-- Valgrind library directory: /usr/lib/valgrind
--19536-- Reading syms from /lib/ld-2.5.so (0x4000000)
--19536-- Reading syms from /home/strecha/test1 (0x8048000)
--19536-- Reading syms from /usr/lib/valgrind/x86-linux/memcheck (0x3800=
0000)
--19536-- object doesn't have a symbol table
--19536-- object doesn't have a dynamic symbol table
--19536-- Reading suppressions file: /usr/lib/valgrind/default.supp
--19536-- REDIR: 0x4015970 (index) redirected to 0x38027EDB (???)
--19536-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_core.s=
o (0x401E000)
--19536-- object doesn't have a symbol table
--19536-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_memche=
ck.so (0x4021000)
--19536-- object doesn't have a symbol table
=3D=3D19536=3D=3D WARNING: new redirection conflicts with existing -- ig=
noring it
--19536-- new: 0x04015970 (index ) R-> 0x040242C0 index
--19536-- REDIR: 0x4015B10 (strlen) redirected to 0x4024370 (strlen)
--19536-- Reading syms from /lib/libc-2.5.so (0x4047000)
--19536-- REDIR: 0x40B2130 (rindex) redirected to 0x40241A0 (rindex)
--19536-- REDIR: 0x40AF470 (realloc) redirected to 0x40235DF (realloc)
--19536-- REDIR: 0x40B34B0 (memcpy) redirected to 0x40252E0 (memcpy)
=3D=3D19536=3D=3D Invalid write of size 1
=3D=3D19536=3D=3D at 0x402540A: memcpy (in /usr/lib/valgrind/x86-linu=
x/vgpreload_memcheck.so)
=3D=3D19536=3D=3D by 0x8048482: main (test1.c:15)
=3D=3D19536=3D=3D Address 0x202800 is not stack'd, malloc'd or (recentl=
y) free'd
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D Process terminating with default action of signal 11 (=
SIGSEGV)
=3D=3D19536=3D=3D Access not within mapped region at address 0x202800
=3D=3D19536=3D=3D at 0x402540A: memcpy (in /usr/lib/valgrind/x86-linu=
x/vgpreload_memcheck.so)
=3D=3D19536=3D=3D by 0x8048482: main (test1.c:15)
--19536-- REDIR: 0x40ACCF0 (free) redirected to 0x402314A (free)
--19536-- REDIR: 0x40B2FB0 (memset) redirected to 0x40246D0 (memset)
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3=
from 1)
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D 1 errors in context 1 of 1:
=3D=3D19536=3D=3D Invalid write of size 1
=3D=3D19536=3D=3D at 0x402540A: memcpy (in /usr/lib/valgrind/x86-linu=
x/vgpreload_memcheck.so)
=3D=3D19536=3D=3D by 0x8048482: main (test1.c:15)
=3D=3D19536=3D=3D Address 0x202800 is not stack'd, malloc'd or (recentl=
y) free'd
--19536--
--19536-- supp: 3 dl-hack3
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D IN SUMMARY: 1 errors from 1 contexts (suppressed: 3 fr=
om 1)
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D malloc/free: in use at exit: 2,107,392 bytes in 1 bloc=
ks.
=3D=3D19536=3D=3D malloc/free: 2,059 allocs, 2,058 frees, 2,171,668,480 =
bytes allocated.
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D searching for pointers to 1 not-freed blocks.
=3D=3D19536=3D=3D checked 66,516 bytes.
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D 2,107,392 bytes in 1 blocks are definitely lost in los=
s record 1 of 1
=3D=3D19536=3D=3D at 0x40236B2: realloc (in /usr/lib/valgrind/x86-lin=
ux/vgpreload_memcheck.so)
=3D=3D19536=3D=3D by 0x804845C: main (test1.c:14)
=3D=3D19536=3D=3D
=3D=3D19536=3D=3D LEAK SUMMARY:
=3D=3D19536=3D=3D definitely lost: 2,107,392 bytes in 1 blocks.
=3D=3D19536=3D=3D possibly lost: 0 bytes in 0 blocks.
=3D=3D19536=3D=3D still reachable: 0 bytes in 0 blocks.
=3D=3D19536=3D=3D suppressed: 0 bytes in 0 blocks.
--19536-- memcheck: sanity checks: 5 cheap, 1 expensive
--19536-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--19536-- memcheck: auxmaps: 0 searches, 0 comparisons
--19536-- memcheck: SMs: n_issued =3D 32113 (513808k, 501M)
--19536-- memcheck: SMs: n_deissued =3D 30977 (495632k, 484M)
--19536-- memcheck: SMs: max_noaccess =3D 65535 (1048560k, 1023M)
--19536-- memcheck: SMs: max_undefined =3D 0 (0k, 0M)
--19536-- memcheck: SMs: max_defined =3D 20 (320k, 0M)
--19536-- memcheck: SMs: max_non_DSM =3D 1167 (18672k, 18M)
--19536-- memcheck: max sec V bit nodes: 0 (0k, 0M)
--19536-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--19536-- memcheck: max shadow mem size: 18976k, 18M
--19536-- translate: fast SP updates identified: 1,390 ( 90.2=
%)
--19536-- translate: generic_known SP updates identified: 78 ( 5.0%)
--19536-- translate: generic_unknown SP updates identified: 73 ( 4.7%)
--19536-- tt/tc: 3,181 tt lookups requiring 3,200 probes
--19536-- tt/tc: 3,181 fast-cache updates, 3 flushes
--19536-- transtab: new 1,474 (31,852 -> 518,524; ratio 162:10) =
[0 scs]
--19536-- transtab: dumped 0 (0 -> ??)
--19536-- transtab: discarded 6 (143 -> ??)
--19536-- scheduler: 597,681 jumps (bb entries).
--19536-- scheduler: 5/3,809 major/minor sched events.
--19536-- sanity: 6 cheap, 1 expensive checks.
--19536-- exectx: 30,011 lists, 6 contexts (avg 0 per list)
--19536-- exectx: 4,119 searches, 4,113 full compares (998 per 1000)
--19536-- exectx: 0 cmp2, 3 cmp4, 0 cmpAll
Segmentation fault
~>
~> uname -a
Linux machine 2.6.18.8-0.5-default #1 SMP Fri Jun 22 12:17:53 UTC 2007 i=
686 i686 i386 GNU/Linux
~>
~> ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 8185
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) 879920
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 8185
virtual memory (kbytes, -v) 1670000
file locks (-x) unlimited
~>
|