Valgrind说“绝对泄漏”但是它呢?

时间:2017-12-11 23:31:05

标签: c memory-leaks malloc valgrind free

我正在尝试编写一个用于井字游戏的蒙特卡罗树搜索,但是在运行它时会出现真正的内存问题(例如我的计算机内存不足)。

所以我决定用valgrind调查情况。

下面是valgrind说'绝对泄密'的代码块之一。

void player_init (Player **p, player_t symbol)
{
  *p = (Player *) malloc (sizeof (Player));
  (**p).symbol = symbol;
  (**p).score = 0;
}

void player_init_all (Player ***p)
{
  *p = (Player **) malloc (sizeof (Player *) * 2);
  for (int i = 0; i < 2; i++)
    {
      (*p)[i] = (Player *) malloc (sizeof (Player));
    }
  player_init (&(*p)[0], PLAYER1);
  player_init (&(*p)[1], PLAYER2);
}

void player_destroy (Player *p)
{
  free (p);
}

其中Playerplayer_t

typedef char player_t;
typedef struct player Player;
struct player {
    player_t symbol;
    unsigned score;
};

他们以这种方式使用;

int main (int argc, char** argv)
{
    Player **players;
    player_init_all (&players);

    // OTHER FANCY CODE HERE

    for (int i = 0; i < 2; i++)
      player_destroy (players[i]);

    free (players);
    free (board);
    return 0;
}

我是否以错误的方式传递指针?

Valgrind Dump;

==21657== 16 bytes in 1 blocks are definitely lost in loss record 1 of 15
==21657==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21657==    by 0x40147D: player_init_all (main.c:348)
==21657==    by 0x401698: main (main.c:426)
==21657== 
==21657== 16 bytes in 2 blocks are definitely lost in loss record 2 of 15
==21657==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21657==    by 0x4014AF: player_init_all (main.c:351)
==21657==    by 0x401698: main (main.c:426)

其中Line 348player_init_all的开头Line 351 for

中的player_init_all循环

2 个答案:

答案 0 :(得分:6)

这里有一个相当明显的内存泄漏

void player_init_all (Player ***p)
{
  *p = (Player **) malloc (sizeof (Player *) * 2);
  for (int i = 0; i < 2; i++)
    {
      (*p)[i] = (Player *) malloc (sizeof (Player));
    }
  player_init (&(*p)[0], PLAYER1);
  player_init (&(*p)[1], PLAYER2);
}

您为上述循环中的Player个对象分配内存。但是在player_init之后会立即重新分配它并覆盖(*p)[0](*p)[1]的值,肯定会泄漏您在上述循环中分配的内容。

答案 1 :(得分:3)

malloc数组{/ 1}}的空间足够Players

*p = (Player **) malloc (sizeof (Player *) * 2);

然后malloc 3 Player结构:

(*p)[i] = (Player *) malloc (sizeof (Player));

然后在您的init功能malloc再次Players

*p = (Player *) malloc (sizeof (Player));

这会覆盖最初的malloc,它会被泄露,永远不会freed