Search code examples
cmemory-leakspthreadspthread-join

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. How can I avoid such memory leak?

Valgrind output:

==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;
}

Solution

  • You have correctly identified a drawback of using pthread_cancel(): any resource not released/freed by the thread cleanup routine will subsequently leak. 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. 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;
    }