在C语言编程的pthread内存泄漏 [英] pthread memory leak in C programming

查看:418
本文介绍了在C语言编程的pthread内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code。

 无效* timer1_function(无效* EIT);
定时器1的pthread_t;
INT thread_check1 = 0;line72:thread_check1 =在pthread_create(安培;定时器1,NULL,timer1_function,NULL);

Valgrind的显示下面的输出,并说有在问题行72 。什么是错与上面的在pthread_create 使用情况如何?

  1块272个字节可能迷失在负的战绩2 5
  在main.c中主要:72
  1:释放calloc在/usr/lib/valgrind/vg$p$pload_memcheck-amd64-linux.so
  2:在_dl_allocate_tls /build/buildd/eglibc-2.15/elf/dl-tls.c:297
  3:在pthread_create @@ GLIBC_2.2.5在/build/buildd/eglibc-2.15/nptl/allocatestack.c:571
  4:主要上述< 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

这记忆是不是在线程退出清理的原因是,这些数据包含的信息,如线程的退出状态,其父母可能要在以后退房。因此,不要加入 ING线程意味着永远清理内存。

不连接线程的概念类似于僵尸进程

I have the code below.

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

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

Valgrind shows the output below and says that there is a problem in the line 72. what is wrong with the pthread_create usage above?

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>

解决方案

When you create a thread, you allocate some memory with it. The task of cleaning up this memory is done through a call to pthread_join.

The reason this memory is not cleaned up upon thread exit is that these data contain information such as "thread's exit status", which the parent may want to check out later. Therefore, never joining the thread means never cleaning up that memory.

The concept of un-joined threads are similar to zombie processes.

这篇关于在C语言编程的pthread内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆