I am getting a segmentation fault inside of the malloc() routine. Here is the stacktrace from gdb:
#0 0x00007ffff787e882 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff787fec6 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff7882a45 in malloc () from /lib/x86_64-linux-gnu/libc.so.6
#3 0x0000000000403ab0 in xmalloc (size=1024) at global.c:14
#4 0x00000000004020fb in processConnectionQueue (arguments=0x60a4e0)
at connection.c:117
#5 0x00007ffff7bc4e9a in start_thread ()
from /lib/x86_64-linux-gnu/libpthread.so.0
#6 0x00007ffff78f24bd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#7 0x0000000000000000 in ?? ()
What’s going on? What could cause malloc() to segfault?
EDIT: Here is the code from xmalloc(). It’s pretty standard, and as you can see from the stacktrace it’s calling malloc with a size of 1024.
void* xmalloc(size_t size)
{
void* result = malloc(size);
if(!result)
{
if(!size)
{
result = malloc(1);
}
if(!result)
{
fprintf(stderr, "Error allocating memory of size %zu\n", size);
exit(-1);
}
}
return result;
}
And line 117 in connection.c:
item->readBuffer = xmalloc(kInitialPacketBufferSize);
You are most likely seeing the effect of an error elsewhere in your code, that access memory outside an allocation. If you are lucky enough, your code can touch some of the internal values malloc uses to track allocations.
If you have the possibility, try linking your code with an allocation checker like libefence or similar, and use this to locate the real problem.