【发布时间】:2011-11-08 20:15:09
【问题描述】:
每当我在我的程序上运行 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);
如果有人能帮我弄清楚为什么这些都不起作用,我将不胜感激。
【问题讨论】:
标签: c memory-leaks pthreads