当先前 malloc 的结构被释放时,Valgrind 总是抱怨存在内存错误。该结构如下所示:
typedef struct bullet
{
int x, y;
struct bullet * next;
} BULLET;
...我通过使用分配内存
BULLET * b;
b = malloc(sizeof(BULLET)); // sizeof(BULLET) is 16
稍后,只需调用free(b);
. 然而,Valgrind 似乎对此并不满意,所以它告诉我
==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)
当然我不能只分配 8 个字节,因为那将是存储指针所需的大小,而不是结构的大小 - 那么为什么 Valgrind 一直告诉我有错误?
编辑:更多可能相关的代码......
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);
}
}
}
}