分离的pthread_create()会泄漏内存 [英] Detached pthread_create() leaks memory

查看:65
本文介绍了分离的pthread_create()会泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Linux环境中用C开发应用程序.我注意到以下代码仅执行数百次后就会泄漏大量内存:

I'm developping an application in C in a Linux environment. I've noticed that the following code leaks tons of memory after only hundreds executions :

do {
    pthread_t               flushThread;
    pthread_attr_t          attr;
    logevent_thread_t       logThread = { Db , &do_curl };

    if (( pthread_attr_init ( &attr ) == 0 ) &&
        ( pthread_attr_setdetachstate ( &attr , PTHREAD_CREATE_DETACHED ) == 0 )  ) {
        pthread_create ( &flushThread , &attr , (void*)FlushThread , (void*)&logThread );
        pthread_attr_destroy ( &attr );
    }
} while(1);

当我从代码开始时,我只使用了pthread_create(),但是当我注意到泄漏时,我就启动了google's并搜索了StackOverflow,并找到了以下URL:

When I started with the code, I only used pthread_create(), but when I noticed the leak, I started google'd and searched StackOverflow, and found the following URLs :

  • pthread_create memory leak
  • valgrind memory leak errors when using pthread_create

这就是为什么我初始化属性并启动分离"线程的原因.我还破坏了属性. 我不能使用pthread_join(),因为我不想阻塞调用,我希望我的线程自己生活.

That's why I initialize the attributes and start the thread "detached". I also destroy the attributes. I can not use pthread_join() since I don't want a blocking call, I want my thread to live on his own.

不幸的是,泄漏仍然存在.我没有更多的想法了,会得到任何进一步的建议!

Unfortunately, the leak is still there. I've no more ideas, and will get any further advice !

谢谢!

@arrowdodger:不设置任何类型的属性而调用pthread_detach()也会泄漏. 我也尝试使用setdetach和pthread_detach()失败.

@arrowdodger: Calling pthread_detach() without setting any kind of attributes also leaks. I also tried with setdetach and pthread_detach() without success.

@drhirsch:我知道它会泄漏,因为当我将其运行1天时,会出现内存不足"内核恐慌.此外,使用top可以看到越来越多的备忘录专门用于我的过程(但是我知道,最好的方法是使用valgrind).

@drhirsch : I know it leaks because when I have it run for 1 day, I get a "Out Of Memory" kernel panic. Additionnaly, using top I can see more and more memoery dedicated to my process (but I understand the best way to minitor this is to use valgrind).

推荐答案

我从您的链接中了解到,您应该调用pthread_detach()而不是使用DETACHED属性创建它.

What i read from your links is that you should call pthread_detach(), not create it with DETACHED attribute.

这篇关于分离的pthread_create()会泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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