pthread_create内存泄漏?

发布于 2024-12-02 17:11:48 字数 2329 浏览 1 评论 0原文

每当我在程序上运行 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);

我想我可能已经用错误的连接编码了下一个......我要通过 https://computing.llnl.gov/tutorials/pthreads/ 他们将所有线程都放在一个数组中,因此他们只是 for 循环。但我没有将它们全部放在一个数组中,所以我尝试将其更改为工作。请告诉我我是否做错了。

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

如果有人可以帮助我弄清楚为什么这些都不起作用,我将非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-12-09 17:11:48

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

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.

阳光下的泡沫是彩色的 2024-12-09 17:11:48

您可以通过使用使用 pthread_join 的代码并重复创建/连接过程几次来证明不存在泄漏。您会看到“泄漏”的内存量没有变化,证明实际上根本没有内存泄漏。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文