C:valgrind报告中的线程显示了无法释放的额外分配

时间:2014-10-16 07:22:11

标签: c pthreads malloc valgrind

我在C中编写了一个程序。在for循环中它创建了12个线程。

   for (i = 0; i < 12; i++)
   {
        status=pthread_create(&ntid[i],NULL,th_f,NULL);
        if (status != 0)
        {
                printf("Error in Creating Thread\n");
                exit(1);
        }
   }

然后我按照所示加入了他们:

   for (i = 0; i < 12; i++)
   {
                ret=pthread_join(ntid[i],&retval);
        if (ret)
        {
                printf("Error joining\n");
                exit(1);
        }
        printf("Thread terminated with status %d\n", *((int*)retval));
        free (retval);
   }

线程正在执行的函数是th_f。它有1个malloc,它在加入后被释放(正如你在`free(retval)中看到的那样;

关于该计划的valgrind报告如下:

==27123== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==27123== malloc/free: in use at exit: 1,552 bytes in 5 blocks.
==27123== malloc/free: 29 allocs, 24 frees, 4,864 bytes allocated.
==27123== For counts of detected errors, rerun with: -v
==27123== searching for pointers to 5 not-freed blocks.
==27123== checked 89,896 bytes.

==27123== LEAK SUMMARY:
==27123==    definitely lost: 0 bytes in 0 blocks.
==27123==      possibly lost: 0 bytes in 0 blocks.
==27123==    still reachable: 1,552 bytes in 5 blocks.
==27123==         suppressed: 0 bytes in 0 blocks.

所以我需要回答这些问题:

  1. 应该只有12个分配和12个免费。为什么数字加倍?
  2. 无论创建多少线程,5个分配总是额外的。他们没有被释放。为什么?

1 个答案:

答案 0 :(得分:0)

额外的分配和释放可能是线程堆栈。这五个额外的内容可能是由线程库在内部使用的 - 正如它所说的那样,它们不会被泄露但仍然可以访问。