一个独立的pthread导致内存泄漏 [英] A detached pthread causes memory leaks

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

问题描述

有一个<一个href=\"http://stackoverflow.com/questions/5610677/valgrind-memory-leak-errors-when-using-pthread-create\">known内存泄漏,终止与运行未分离pthreads的一个过程时。然而,分离线程也似乎不是一个解决方案。请看下面的小例子:

There is a known memory leak, when terminating a process with running undetached pthreads. However, detaching the thread doesn't seem to be a solution. Consider the following minimal example:

#include <pthread.h>
#include <stdio.h>

static void* thread(void* _) {
  for(;;); return NULL;
}

int main(void) {
  pthread_attr_t attr; 
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  pthread_t tid; pthread_create(&tid, &attr, thread, NULL);
  pthread_attr_destroy(&attr);
  return 0;
}

创建具有无限循环已分离的线程和处理立即终止。根据 pthread_detach(3),线程的资源应自动一旦整个处理结束释放回系统。然而,这显然不是发生了什么:

A detached thread with an infinite loop is created and the process is immediately terminated. According to pthread_detach(3), the resources of the thread should be automatically released back to the system once the entire process is terminated. That, however, clearly isn't what's happening:

gcc -pthread c.c
valgrind --leak-check=full a.out

==9341== Command: a.out
==9341==
==9341==
==9341== HEAP SUMMARY:
==9341==     in use at exit: 272 bytes in 1 blocks
==9341==   total heap usage: 1 allocs, 0 frees, 272 bytes allocated
==9341==
==9341== 272 bytes in 1 blocks are possibly lost in loss record 1 of 1
==9341==    at 0x4C2ABB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9341==    by 0x4012598: _dl_allocate_tls (dl-tls.c:296)
==9341==    by 0x4E3C7B5: pthread_create@@GLIBC_2.2.5 (allocatestack.c:579)
==9341==    by 0x400825: main (in /home/witiko/a.out)
==9341==
==9341== LEAK SUMMARY:
==9341==    definitely lost: 0 bytes in 0 blocks
==9341==    indirectly lost: 0 bytes in 0 blocks
==9341==      possibly lost: 272 bytes in 1 blocks
==9341==    still reachable: 0 bytes in 0 blocks
==9341==         suppressed: 0 bytes in 0 blocks

我应该关心?在实际的程序我有几个阻塞线程,因此,很像最小的例子,我真的不能在pthread_join()他们。我应该叫 pthread_cancel可以()而不是退出()直接ING?

Should I be concerned? In the actual program I have several blocking threads, so, much like in the minimal example, I can't really pthread_join() with them. Should I be calling pthread_cancel() instead of exit()ing directly?

推荐答案

等同于退出的全过程,所以这实际上是相当终止您的分离线程粗鲁的方式。你的线程根本没有当函数结束终止,它只是这样做后,当退出机制迫使其。这样的valgrind缺少的线程的资源的释放。

Returning from main is equivalent to an exit of the whole process, so this is in effect quite a rude way to terminate your detached thread. Your thread simply hasn't terminated when the main function ends, it only does so later when the exit mechanism forces it. So valgrind is missing the release of the resources of the thread.

这的valgrind告诉你,有内存泄漏不应该自己担心你,但你的线程,而能够清理和/或完成其任务终止的事实应该担心你。事实上

The fact that valgrind tells you that there is leaking memory shouldn't worry you by itself, but the fact that your thread is terminated without being able to cleanup and/or finish its task should worry you.

如果你想有你的线程继续你的线头执行后,你应该终止由了pthread_exit 主而不是收益。然后,它是由你分离的线程来决定何时终止自己。它可以决定这样,通过被设置原子或通过互斥/条件机构的状态变量接收必要的信息。

If you want to have your thread continue execution after your main thread ends, you should terminate main by pthread_exit instead of return. Then it is up to your detached thread to decide when to terminate itself. It could decide so, on receiving the necessary information through a state variable that is set atomically or through a mutex/condition mechanism.

这篇关于一个独立的pthread导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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