分离的pthread和内存泄漏 [英] Detached pthreads and memory leak

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

问题描述

有人可以向我解释为什么这个简单的代码泄漏内存?



我相信,因为pthreads是创建分离状态,他们的资源应该在终止后立即释放,但不是这样。



我的环境是Qt5.2。

  #include< QCoreApplication> 
#include< windows.h>

void * threadFunc(void * arg)
{
printf(#);
pthread_exit(NULL);
}

int main()
{
pthread_t thread;
pthread_attr_t attr;

while(1)
{
printf(\\\
Starting threads ... \\\
);
for(int idx = 0; idx< 100; idx ++)
{
pthread_attr_init(& attr);
pthread_attr_setdetachstate(& attr,PTHREAD_CREATE_DETACHED);
pthread_create(& thread,& attr,& threadFunc,NULL);
pthread_attr_destroy(& attr);
}
printf(\\\
Sleeping 10 seconds ... \\\
);
Sleep(10000);
}
}



UPDATE:



我发现如果我在中为循环增加了5毫秒的微小延迟,那么泄漏是 WAY 更慢: / p>

  for(int idx = 0; idx< 100; idx ++)
{
pthread_attr_init(& attr );
pthread_attr_setdetachstate(& attr,PTHREAD_CREATE_DETACHED);
pthread_create(& thread,& attr,& threadFunc,NULL);
pthread_attr_destroy(& attr);
Sleep(5); ///< --- 5 MILLISECONDS DELAY ///
}


这是让我失望,有人可以告诉我发生了什么?这种轻微的延迟会如何产生这样的重大变化? (或以任何方式改变行为)



任何建议都将非常感激。



p>

UPDATE2:



在Windows平台(W7和XP)在Linux平台上没有观察到泄漏。(感谢@MichaelGoren)

解决方案

和内存消耗稳定。所以它必须是一个qt问题; cygwin上的pthread库工作正常,没有泄漏。

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


void * threadFunc(void * arg)
{
printf(#);
pthread_exit(NULL);
}

int main()
{
pthread_t thread;
pthread_attr_t attr;
int idx;

while(1)
{
printf(\\\
Starting threads ... \\\
);
for(idx = 0; idx< 100; idx ++)
{
pthread_attr_init(& attr);
pthread_attr_setdetachstate(& attr,PTHREAD_CREATE_DETACHED);
pthread_create(& thread,& attr,& threadFunc,NULL);
pthread_attr_destroy(& attr);
}
printf(\\\
Sleeping 10 seconds ... \\\
);
// Sleep(10000);
sleep(10)
}
}


Can somebody please explain to me why this simple code leaks memory?

I believe that since pthreads are created with detached state their resources should be released inmediatly after it's termination, but it's not the case.

My environment is Qt5.2.

#include <QCoreApplication>
#include <windows.h>

void *threadFunc( void *arg )
    {
    printf("#");
    pthread_exit(NULL);
    }

int main()
    {
    pthread_t thread;
    pthread_attr_t attr;

    while(1)
        {
        printf("\nStarting threads...\n");
        for(int idx=0;idx<100;idx++)
            {
            pthread_attr_init(&attr);
            pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
            pthread_create( &thread, &attr, &threadFunc, NULL);
            pthread_attr_destroy ( &attr );
            }
        printf("\nSleeping 10 seconds...\n");
        Sleep(10000);
        }
    }

UPDATE:

I discovered that if I add a slight delay of 5 milliseconds inside the for loop the leak is WAY slower:

    for(int idx=0;idx<100;idx++)
        {
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        pthread_create( &thread, &attr, &threadFunc, NULL);
        pthread_attr_destroy ( &attr );
        Sleep(5); /// <--- 5 MILLISECONDS DELAY ///
        }

This is freaking me out, could somebody please tell me what is happening? How this slight delay may produce such a significant change? (or alter the behavior in any way)

Any advice would be greatly appreciated.

Thanks.

UPDATE2:

This leak was observed on Windows platforms (W7 and XP), no leak was observed on Linux platforms (thank you @MichaelGoren)

解决方案

I checked the program with slight modifications on windows using cygwin, and memory consumption was steady. So it must be a qt issue; the pthread library on cygwin works fine without leaking.

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


void *threadFunc( void *arg )
{
printf("#");
pthread_exit(NULL);
}

int main()
{
pthread_t thread;
pthread_attr_t attr;
int idx;

while(1)
    {
    printf("\nStarting threads...\n");
    for(idx=0;idx<100;idx++)
        {
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        pthread_create( &thread, &attr, &threadFunc, NULL);
        pthread_attr_destroy ( &attr );
        }
    printf("\nSleeping 10 seconds...\n");
    //Sleep(10000);
sleep(10);
    }
}

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

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