c - C编程中的pthread内存泄漏

标签 c memory-leaks pthreads

我有下面的代码。

void *timer1_function(void * eit);
pthread_t timer1;
int thread_check1 = 0;

line72: thread_check1 = pthread_create( &timer1, NULL, timer1_function,  NULL);

Valgrind 显示下面的输出并指出 第 72 行 中存在问题。上面的 pthread_create 用法有什么问题?

272 bytes in 1 blocks are possibly lost in loss record 2 of 5
  in main in main.c:72
  1: calloc in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
  2: _dl_allocate_tls in /build/buildd/eglibc-2.15/elf/dl-tls.c:297
  3: pthread_create@@GLIBC_2.2.5 in /build/buildd/eglibc-2.15/nptl/allocatestack.c:571
  4: main in <a href="file:///home/user/Project-build-desktop-Qt_4_8_1_in_PATH__System__Release/../project/main.c:72" >main.c:72</a>

最佳答案

当您创建一个线程时,您会为其分配一些内存。清理此内存的任务是通过调用 pthread_join 完成的。

此内存在线程退出时未被清理的原因是这些数据包含诸如“线程的退出状态”之类的信息,父级可能希望稍后查看这些信息。因此,从不加入线程意味着从不清理该内存。

未加入线程的概念类似于zombie processes .

关于c - C编程中的pthread内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123823/

相关文章:

c - 在 GNU/Linux 上链接插件时如何巧妙地检测 undefined symbol ?

c++ - 内存泄漏/管理建议(方法中的新数组)(c++)

c - 使用pthread执行矩阵乘法

c - 关于在C中将 `void *`转换为 `int`的问题

在线程中计算 Pi

c - 函数指针存放在程序的哪一段?

c++ - 为什么比较运算符这么快?

c - 为什么下面的代码中有竞争条件

c++ - 当一个类的所有实例都被销毁时释放内存

java - 整个应用程序中的一个插页式广告