Meaning of "free(): *some error*"

I keep running into runtime errors that start with "free(): *some text*. I would like some help in understanding what these errors mean. For example, I know that when a program segfaults, I generally have an issue with a pointer accessing memory it shouldn't be, or I've dereferenced a null pointer somewhere. My research on the free() errors hasn't turned up much in the way of general guideline (I do find specific issues in forum posts, but am having problems making the jump to the general), and gdb (admittedly, I'm not much of an expert with it) doesn't really help with this type of error, either; it doesn't seem to indicate a line number or anything the program aborted.

Query: What, in general, does a free(): error signify?
Followup: What tools within gdb (if any) can help me track down the source of this behavior?
free is C for delete of a pointer.
if you attempt to delete a pointer that you do not own, it will blow up.
if you attempt to delete a pointer that is not dynamic, it will blow up.
consider:

int x;
int *ip = &x;
free(x);

pointer problems can be, and usually are, hard to track down. The first step is compile with every possible complaint enabled in the compiler, and then look at and fix warnings etc. You can also try setting all deleted pointers immediately to null so you do not try to delete them again, as delete on null is safe/harmless. Often this bug is double delete of same pointer.
Last edited on
Post the smallest program that demonstrates the problem so we can run it.
Also, what system/compiler are you using?
Last edited on
Solved the issue I was having. I was writing a function that copied a "journal entry" object into a linked list, but failed to use the copy function I wrote into the entry object to actually copy the entry into the node. I suspect that the default deconstructor for the list then tried to delete the original object(which would no longer exist by the point where the list deconstructor started up), causing a double deletion. The code would be tricky to post- a bunch of it is hidden from me, supplied as a .o file (class project). But this gave me what I needed to solve my issue, so thank you!
> "free(): *some text*
there are some cases where it pays off to read what "some tex" actually says


> What tools within gdb (if any) can help me track down the source of this behavior?
valgrind is quite good at detecting bad free/delete, invalid pointer access, memory leaks
you may also sync it with gdb
https://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-simple
Topic archived. No new replies allowed.