0
votes

I'm running a program through gdb. It runs the line "free(buffer);" then I get this message: Program received signal SIGSEGV, Segmentation fault.

 0xb7e97103 in __GI___libc_free (mem=0xbffff11e) at malloc.c:2987
 2987     ar_ptr = arena_for_chunk(p);

The next line in the program is "fclose(inptr);" How do I start solving this problem?

2
Did either of the answers help you? If so, it would be constructive to accept one (click on the tick) and upvote it (click on the up triangle). If they both helped, upvote both and tick the most useful one. If neither helped, explain why and I'm sure you'll get more help ... - Neil Townsend

2 Answers

1
votes

your variable buffer is either null or points to memory that cannot be freed (or has already been freed).

1
votes

The actual answer to your question is that ar_ptr = arena_for_chunk(p); is an internal data structure that determines where the memory should be freed to. However, you can keep digging there if you like, until you find why p which almost certainly is the buffer you pass in or some value closely related to it is "incorrect" - but that would be a bit like looking for your dropped keys under the streetlamp because you can see better there, rather than trying to find them where you dropped them.

When code in the standard library crashes, especially code that is called every time you run any trivial program, then it's 99.9% of the time caused by "bad input" - go look at the code calling the function, not inside the function (although it's worth looking back at whatever went wrong to determine which of the input parameters it was that caused the problem).

The actual problem is that your code is calling free() with a bad parameter, which makes the arena_for_chunk to be doing something wrong - given the value mem = 0xbffff11e in the line above, I expect that buffer is actually a value on the stack, which shouldn't be freed.