pthread_create 内存泄漏?

Ste*_*ing 3 c memory-leaks pthreads

每当我在我的程序上运行 valgrind 时,它表明我可能在调用 pthread_create 的地方丢失了内存。我一直在努力遵循指导

使用 pthread_create 时的 valgrind 内存泄漏错误 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);
Run Code Online (Sandbox Code Playgroud)

我想我可能已经在下一个代码中加入了错误的代码......我要去 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);
Run Code Online (Sandbox Code Playgroud)

尝试 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);
Run Code Online (Sandbox Code Playgroud)

尝试 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);
Run Code Online (Sandbox Code Playgroud)

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

R..*_*R.. 5

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