Search code examples
cpthreadsmallocfree

Unable to properly free allocated memory on heap, detected by Valgrind


I am have an issue with freeing my allocated memory, and it seems that my inexperience has lead me to this fatal error.

Below I have a simple code:

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

void *thread(void *arg) {
    return NULL;
}

int main() {
    pthread_t id;
    pthread_t *pid = malloc(10 * (sizeof(pthread_t)));
    for (int i = 0; i < 10; i++) {
        pthread_create(&id, NULL, thread, NULL);
        pid[i] = id;
    }
    free(pid); 
}

So, apprently free(pid) does not free the 10 threads I created as well and as a result valgrind is telling me that I only free 1 out of 11 allocs. how do I go about free the 10 threads?

EDIT: I think I need to store the address of the 10 threads and then free those in a for loop if I am correct here?

EDIT#2:

I have tried the following:

  for (int i = 0; i < 10; i++) {
      free(pid[i]);
  } 

but I get this error

/usr/include/stdlib.h:483:13: note: expected ‘void *’ but argument is of type ‘pthread_t’
 extern void free (void *__ptr) __THROW;

Solution

  • The extra malloc()s are used by pthread_create(). You'll need to wait for your threads to die for them to be freed, for instance with pthread_join(). You must not call free() on them.

    For instance:

    #define _POSIX_C_SOURCE 200809L
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    void* thread(void* arg)
    {
        return NULL;
    }
    
    int main(){
        pthread_t id;
        pthread_t *pid = malloc(10 *(sizeof(pthread_t)));
        for(int i=0; i<10; i++) {
            pthread_create(&id, NULL, thread, NULL);
            pid[i] = id;
            pthread_join(id, NULL);
        }
    
        free(pid); 
    }