1

Possible Duplicate:
Segmentation fault in btree implementation

I get an error like this, How can I debug it? Can you please give some some method to debug this error:

  *** glibc detected *** ./a.out: free(): invalid pointer: 0x0821b158 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0xbd7591]
    /lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0xbd8de8]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xbdbecd]
    ./a.out[0x80490c3]
    ./a.out[0x8048bdc]
    ./a.out[0x8048642]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb82bd6]
    ./a.out[0x80484b1]
    ======= Memory map: ========
    004e5000-00500000 r-xp 00000000 08:06 1192669    /lib/ld-2.11.1.so
    00500000-00501000 r--p 0001a000 08:06 1192669    /lib/ld-2.11.1.so
    00501000-00502000 rw-p 0001b000 08:06 1192669    /lib/ld-2.11.1.so
    007aa000-007c7000 r-xp 00000000 08:06 1179731    /lib/libgcc_s.so.1
    007c7000-007c8000 r--p 0001c000 08:06 1179731    /lib/libgcc_s.so.1
    007c8000-007c9000 rw-p 0001d000 08:06 1179731    /lib/libgcc_s.so.1
    0096b000-0096c000 r-xp 00000000 00:00 0          [vdso]
    00b6c000-00cbf000 r-xp 00000000 08:06 1311379    /lib/tls/i686/cmov/libc-2.11.1.so
    00cbf000-00cc0000 ---p 00153000 08:06 1311379    /lib/tls/i686/cmov/libc-2.11.1.so
    00cc0000-00cc2000 r--p 00153000 08:06 1311379    /lib/tls/i686/cmov/libc-2.11.1.so
    00cc2000-00cc3000 rw-p 00155000 08:06 1311379    /lib/tls/i686/cmov/libc-2.11.1.so
    00cc3000-00cc6000 rw-p 00000000 00:00 0 
    08048000-0804a000 r-xp 00000000 08:06 393339     /home/user/Desktop/a.out
    0804a000-0804b000 r--p 00001000 08:06 393339     /home/user/Desktop/a.out
    0804b000-0804c000 rw-p 00002000 08:06 393339     /home/user/Desktop/a.out
    0821b000-0823c000 rw-p 00000000 00:00 0          [heap]
    b7600000-b7621000 rw-p 00000000 00:00 0 
    b7621000-b7700000 ---p 00000000 00:00 0 
    b7708000-b7709000 rw-p 00000000 00:00 0 
    b771a000-b771e000 rw-p 00000000 00:00 0 
    bfc39000-bfc4e000 rw-p 00000000 00:00 0          [stack]
    Aborted
Community
  • 1
  • 1
lal
  • 23
  • 4
  • 5
    the problem is in your code, show it to us – BlackBear Mar 29 '11 at 12:20
  • 3
    `*** glibc detected *** ./a.out: free(): invalid pointer: 0x0821b158 ***` check your calls to `free()` – Will Tate Mar 29 '11 at 12:22
  • 3
    When free() complains about an invalid pointer, you might have tried to free something that wasn't malloc'ed, or perhaps free the same memory twice. – Bo Persson Mar 29 '11 at 12:23
  • 3
    To be fair, @lal actually asked how to debug his problem, not that we solve it. – johnny Mar 29 '11 at 12:23
  • @ BlackBear http://stackoverflow.com/questions/5471765/segmentaion-fault this the question i put up previously but i didn't get help so thought of getting some idea from the best how to solve this and try once again the way suggested here in stack overflow – lal Mar 29 '11 at 13:04
  • http://stackoverflow.com/questions/5472379/segmentation-fault @ BlackBear stackoverflow.com/questions/5471765/segmentaion-fault this the question i put up previously but i didn't get help so thought of getting some idea from the best how to solve this and try once again the way suggested here in stack overflow – – lal Mar 29 '11 at 13:07

2 Answers2

5

Build with debug on (gcc -g *.c) and then look at your stack.

Specifically this error is because you called free on a bad pointer.

Some code might help us look at your problem more specifically.

Starkey
  • 9,673
  • 6
  • 31
  • 51
  • http://stackoverflow.com/questions/5472379/segmentation-fault?tab=active#tab-top @Starkey: i have put up the code in this link.this the question i put up previously but i didn't get help so thought of getting some idea from the best how to solve this and try once again the way suggested here in stack overflow – lal Mar 29 '11 at 13:08
1

You'll probably find that the easiest way of debugging errors like this is with the use of a debugger.

As Starkey suggested, make sure that you enable debug symbols when you compile (through the -g option to gcc).

You may find that when your program crashed with the segmentation fault that it generated a core file. You can use a debugger (eg. gdb) to open the core file and to investigate the call stack. The advantage of using the debugger is that it will (if you've enabled debug symbols) show you the line numbers within your source files, rather than just providing memory locations.

If you don't have a core file you can run your application from within gdb and then reproduce the segmentation fault.

Andrew Edgecombe
  • 39,594
  • 3
  • 35
  • 61