Search code examples
c++valgrind

Valgrind results of a "segmentation fault" program


My program (./a.out) encountered with a segmentation fault, so I use Valgrind to check if I can find at which line of code the program corrupts. I got the following output, but I cannot understand them. To me, the most suspicious line of the output is ==17967== Address 0x20687cf80 is 0 bytes inside a block of size 16 alloc'd, does this line means the address 0x20687cf80 is not propoerly allocated a memory block? What can I do to resolve this problem.

I am using a 64-bit linux with 64GB ram.

[root@gpu BloomFilterAndHashTable]# valgrind --tool=memcheck --leak-check=full ./a.out /mnt/disk2/experiments/two_stage_bloom_filter/test/10_10.txt /mnt/disk2/experiments/10M_worstcase_trace/w_10_10.trace 24
==17967== Memcheck, a memory error detector
==17967== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==17967== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==17967== Command: ./a.out /mnt/disk2/experiments/two_stage_bloom_filter/test/10_10.txt /mnt/disk2/experiments/10M_worstcase_trace/w_10_10.trace 24
==17967==
9998797 Prefixes loaded!  //output of my program
==17967== Warning: set address range perms: large range [0x4201a040, 0x6f423220) (defined)
==17967== Warning: set address range perms: large range [0x9c834040, 0x20687cf40) (undefined)
insertion cost time(us): 173168519      9998797 17.318935       0.057740 //output of my program
==17967== Warning: set address range perms: large range [0x23647d040, 0x25647d040) (defined)
Trace loaded! //output of my program
lookup cost time(us): 5728767367        67108864        85.365286       0.011714  //output of my program
==17967== Mismatched free() / delete / delete []
==17967==    at 0x4A055FE: free (vg_replace_malloc.c:366)
==17967==    by 0x401B13: hash_table_delete(BloomFilter*, char*) (BloomFilterAndHashTable.cpp:503)
==17967==    by 0x402212: main (BloomFilterAndHashTable.cpp:687)
==17967==  Address 0x20687cf80 is 0 bytes inside a block of size 16 alloc'd
==17967==    at 0x4A05F97: operator new(unsigned long) (vg_replace_malloc.c:261)
==17967==    by 0x40146D: hash_table_insert(char*, int, BloomFilter*) (BloomFilterAndHashTable.cpp:293)
==17967==    by 0x401DD5: main (BloomFilterAndHashTable.cpp:597)
==17967==
Delete succeeded!  //output of my program
deletion cost time(us): 178048113       9998797 17.806953       0.056158  //output of my program
==17967== Warning: set address range perms: large range [0x23647d030, 0x25647d050) (noaccess)
--17967:0:aspacem  Valgrind: FATAL: VG_N_SEGMENTS is too low.
--17967:0:aspacem    Increase it and rebuild.  Exiting now.
[root@gpu BloomFilterAndHashTable]#

Solution

  • The "suspicious" output ==17967== Address 0x20687cf80 is 0 bytes inside a block of size 16 alloc'd means, that there is an allocated block of memory, 16 bytes in size. Address 0x20687cf80 is the address of the very first byte of that block (i.e. it's the address of the whole block). So the line itself does only tell you details about a memory block that is involved in the whole warning.

    The warning itself is about a "mismatched free()". The following lines show where the free was called:

    ==17967== at 0x4A055FE: free (vg_replace_malloc.c:366)
    ==17967== by 0x401B13: hash_table_delete(BloomFilter*, char*) (BloomFilterAndHashTable.cpp:503)

    Meaning, hash_table_delete calls free(). Now, why does valgrind think that this is a mismatch? Because the address of the memory block that gets freed (0x20687cf80) was allocated by operator new, which was called by hash_table_insert:

    ==17967== at 0x4A05F97: operator new(unsigned long) (vg_replace_malloc.c:261)
    ==17967== by 0x40146D: hash_table_insert(char*, int, BloomFilter*) (BloomFilterAndHashTable.cpp:293)

    This is suspicious. If it is the source of your error is another problem, but you should fix it anyways.