简体   繁体   English

如何通过使用pthread_cancel避免内存泄漏?

[英]How to avoid a memory leak by using pthread_cancel?

I have a program which should start a thread. 我有一个应该启动线程的程序。 To avoid to exit the software the thread runs in an endless loop and I join the thread. 为了避免退出该软件,该线程以无限循环运行,因此我加入了该线程。 This thread is never supposed to return a value. 永远不要让该线程返回值。 So now I have the problem that after I call pthread_cancel valgrind detects a memory leak. 因此,现在我遇到的问题是,在调用pthread_cancel之后,valgrind会检测到内存泄漏。 How can I avoid such memory leak? 如何避免这种内存泄漏?

Valgrind output: Valgrind输出:

==5673== 136 bytes in 1 blocks are possibly lost in loss record 4 of 8
==5673==    at 0x4026A68: calloc (vg_replace_malloc.c:566)
==5673==    by 0x40111FB: _dl_allocate_tls (dl-tls.c:300)
==5673==    by 0x404E5A0: pthread_create@@GLIBC_2.1 (allocatestack.c:580)
==5673==    by 0x804C44E: start (mythread.c:25)
==5673==    by 0x804D128: main (main.c:10)

code: 码:

int main(){
    signal(SIGINT,cleanup);
    signal(SIGQUIT,cleanup);
    signal(SIGSEGV,cleanup);
    start();
    return 0;
}

int start()
{
    pthread_create(&thread_id[0],NULL,&threadhandler,NULL);
    pthread_join(thread_id[0],NULL);
    return err;
}

void cleanup(){
   pthread_cancel(thread_id[0]);
   exit(0);
}

void cleanup_tcp(void *p){
}


void* threadhandler(void *arg)
{
    (void) arg;
    int status = 0;
    while(TRUE){
        pthread_cleanup_push(cleanup_tcp,NULL);
        pthread_testcancel();
        fprintf(stderr,"Run\n");
        pthread_cleanup_pop(0);
    }
    return NULL;
}

You have correctly identified a drawback of using pthread_cancel() : any resource not released/freed by the thread cleanup routine will subsequently leak. 您已经正确识别了使用pthread_cancel()的缺点:线程清理例程未释放/释放的任何资源随后都会泄漏。 In your case, it appears that the thread library, itself, might have allocated some memory that isn't being freed. 在您的情况下,似乎线程库本身可能已经分配了一些未释放的内存。

A better approach, IMO, would be to create a mechanism for notifying the threadhandler thread that it should terminate. IMO更好的方法是创建一种机制来通知线程处理threadhandler线程应该终止。 For example 例如

static volatile sig_atomic_t done = 0;
...
void cleanup()
{
    done = 1;
}

void* threadhandler(void* arg)
{
    while (!done)
        fprintf(stderr, "Run\n");
    return NULL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM