Valgrind always nags that there is a memory error when a previously malloc'ed struct is free'd. The struct looks as follows:
typedef struct bullet
{
int x, y;
struct bullet * next;
} BULLET;
... and I allocate the memory by using
BULLET * b;
b = malloc(sizeof(BULLET)); // sizeof(BULLET) is 16
and later on, the struct is free'd by simply calling free(b);
. Valgrind however doesn't seem to be satisfied by this, and so it tells me
==2619== Invalid read of size 8
==2619== at 0x40249F: ctrl_bullets (player.c:89)
==2619== by 0x405083: loop_game (game.c:305)
==2619== by 0x406CCA: main (main.c:47)
==2619== Address 0x5b8d818 is 8 bytes inside a block of size 16 free'd
==2619== at 0x4C29A9E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2619== by 0x402E04: rm_bullet (player.c:329)
==2619== by 0x402485: ctrl_bullets (player.c:95)
==2619== by 0x405083: loop_game (game.c:305)
==2619== by 0x406CCA: main (main.c:47)
Of course I can't just allocate only 8 bytes, because that would be the size needed to store the pointer, and not the size of the struct - so why does Valgrind keep telling me that there is an error?
Edit: Some more code which could be relevant ...
void
ctrl_bullets(WINDOW * w_field, BULLETLIST * lb)
{
if (lb->num > 0)
{
BULLET * b;
for (b = lb->head; b != NULL; b = b->next) // player.c:89
{
if (b->x > CON_FIELDMAXX)
{
write_log(LOG_DEBUG, "Bullet %p is outside the playing field; x: %d; "
"y: %d\n", (void *) b, b->x, b->y);
rm_bullet(w_field, lb, b);
}
else
{
mv_bullet(w_field, b);
}
}
}
}