简体   繁体   English

pthread_create内存泄漏?

[英]pthread_create memory leak?

Whenever I run valgrind on my program, it is telling that I have possibly lost memory wherever I call pthread_create. 每当我在程序上运行valgrind时,它都告诉我在调用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/ 使用pthread_create http://gelorakan.wordpress.com/2007/11/26/pthead_create-valgrind-memory-leak-solved/ 时valgrind内存泄漏错误

and other various websites google gave me, but nothing has worked. 以及Google提供给我的其他各种网站,但没有任何效果。 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(). 到目前为止,我已经尝试加入线程,将pthread_attr_t设置为DETACHED,在每个线程上调用pthread_detach,然后调用pthread_exit()。

trying PTHREAD_CREATE_DETACHED - 尝试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. 我想我可能已经用错误的连接编码了下一个...我正在通过https://computing.llnl.gov/tutorials/pthreads/进行操作 ,它们的所有线程都在数组中,因此它们只是用于循环。 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_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_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. 如果有人可以帮助我弄清楚为什么这些都不起作用,我将不胜感激。

glibc doesn't free thread stacks when threads exit; 当线程退出时,glibc不会释放线程堆栈; it caches them for reuse, and only prunes the cache when it gets huge. 它会缓存它们以供重用,并且只有在变得很大时才修剪缓存。 Thus it always "leaks" some memory. 因此,它总是“泄漏”一些内存。

You can prove there's no leak by taking your code that uses pthread_join and repeating the create/join process a few times. 您可以通过使用pthread_join的代码并重复创建/联接过程几次来证明没有泄漏。 You'll see that the amount of memory "leaked" does not change, proving that no memory was actually leaked at all. 您将看到“泄漏”的内存量没有变化,证明实际上没有任何内存泄漏。

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

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