gpt4 book ai didi

c - pthread_create 内存泄漏?

转载 作者:行者123 更新时间:2023-12-04 16:34:01 29 4
gpt4 key购买 nike

每当我在我的程序上运行 valgrind 时,它都会告诉我在调用 pthread_create 的任何地方都可能丢失了内存。我一直在尝试遵循

上的指导

valgrind memory leak errors when using pthread_create http://gelorakan.wordpress.com/2007/11/26/pthead_create-valgrind-memory-leak-solved/

和谷歌给我的其他各种网站,但没有任何效果。到目前为止,我已经尝试加入线程,将 pthread_attr_t 设置为 DETACHED,在每个线程上调用 pthread_detach,并调用 pthread_exit()。

尝试 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);

我想我可能在加入错误的情况下编写了下一个代码......我正在经历 https://computing.llnl.gov/tutorials/pthreads/他们将所有线程都放在一个数组中,因此它们只是用于循环。但我并没有将它们全部放在一个数组中,所以我试图改变它来工作。如果我做错了请告诉我。

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);

尝试 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);

尝试 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);

如果有人能帮我弄清楚为什么这些都不起作用,我将不胜感激。

最佳答案

当线程退出时,glibc 不会释放线程堆栈;它缓存它们以供重用,并且仅在缓存变大时才对其进行修剪。因此它总是“泄漏”一些内存。

关于c - pthread_create 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7278216/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com