c - C : valgrind report shows additional allocs that can't be freed 中的线程

标签 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 个分配总是额外的。他们没有被释放。为什么?

最佳答案

额外的分配和释放可能是线程堆栈。额外的五个可能由线程库在内部使用——正如它所说,它们没有泄漏但仍然可以访问。

关于c - C : valgrind report shows additional allocs that can't be freed 中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398443/

相关文章:

c - 从 PCM 中删除 channel

无法使用多线程在我的树莓派上编译我的 C 程序

python - f2py 中的 malloc 错误

c - 在无限循环中运行 malloc()

c - 为什么gets函数如此危险以至于不应该使用它?

c - MSP430 执行库

在 C 中将 yuv420p 转换为 rgb888

检查 Linux C 中 pthread 是否仍然存在

c - C中的线程,如何使用它们来移动玩家?

c - 调整可能太大的字符串数组的大小