Reputation: 4223
Whenever I run valgrind on my program, it is telling that I have possibly lost memory wherever I call pthread_create. I have been trying to follow the guidance on
valgrind memory leak errors when using pthread_create http://gelorakan.wordpress.com/2007/11/26/pthead_create-valgrind-memory-leak-solved/
and other various websites google gave me, but nothing has worked. So far I have tried joining the threads, setting an pthread_attr_t to DETACHED, calling pthread_detach on each thread, and calling pthread_exit().
trying PTHREAD_CREATE_DETACHED -
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&c_udp_comm, &attr, udp_comm_thread, (void*)this);
pthread_create(&drive, &attr, driving_thread, (void*)this);
pthread_create(&update, &attr, update_server_thread(void*)this);
I think I may have coded this next one with joining wrong...I am going by https://computing.llnl.gov/tutorials/pthreads/ and they have all their threads in an array so they just for loop. But I don't have them all in an array so I tried to just change it to work. Please tell me if I did it wrong.
void* status;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&c_udp_comm, &attr, udp_comm_thread, (void*)this);
pthread_create(&drive, &attr, driving_thread, (void*)this);
pthread_create(&update, &attr, update_server_thread(void*)this);
pthread_join(c_udp_comm, &status);
pthread_join(drive, &status);
pthread_join(update, &status);
trying pthread_detach -
pthread_create(&c_udp_comm, NULL, udp_comm_thread, (void*)this);
pthread_create(&drive, NULL, driving_thread, (void*)this);
pthread_create(&update, NULL, update_server_thread(void*)this);
pthread_detach(c_udp_comm);
pthread_detach(drive);
pthread_detach(update);
trying pthread_exit -
pthread_create(&c_udp_comm, NULL, udp_comm_thread, (void*)this);
pthread_create(&drive, NULL, driving_thread, (void*)this);
pthread_create(&update, NULL, update_server_thread(void*)this);
pthread_exit(NULL);
If anyone can help me figure out why none of this is working I would be very grateful.
Upvotes: 3
Views: 2783
Reputation: 182893
You can prove there's no leak by taking your code that uses pthread_join
and repeating the create/join process a few times. You'll see that the amount of memory "leaked" does not change, proving that no memory was actually leaked at all.
Upvotes: 2
Reputation: 215647
glibc doesn't free thread stacks when threads exit; it caches them for reuse, and only prunes the cache when it gets huge. Thus it always "leaks" some memory.
Upvotes: 5