pthread内存泄漏与堆栈变量 [英] pthread memory leak with stack variables
问题描述
我注意到,当我调用一个方法使用线程形式
////主代码// ///
pthread_t thread;
pthread_create(thread,function,data);
//////////////////
void * function(void * data){
//工作通过堆栈上的向量在堆上的一些数据
std :: vector< double>变量(100,1.2345);
//做线程的内容
pthread_exit(NULL);尽管没有调用 new $ c>,但是
}
$ c>(除了隐含在向量变量中)我得到一个内存泄漏,内存使用量与我以这种方式调用函数
的次数成线性关系。 p>
但是,如果我这样做
void * function(void * data) {
{
std :: vector< double>变量(100,1.2345);
//做线程的内容
}
pthread_exit(NULL);
}
不会发生内存泄漏。
似乎 pthread_exit(NULL)
不会清除堆栈变量,就像你在正常函数结束时得到的 return
(我是正确的吗?),因此将它们放在自己的范围内可以确保它们被释放。
看起来像一个巨大的kludge。如何在退出pthread时确保堆栈变量(以及它们在容器方面的内容)在正确地被清除?
blockquote>
似乎 pthread_exit(NULL)
不会清除堆栈变量,就像你在正常函数结束时得到的返回值
这就像调用 exit(0)
在非线程代码中,程序立即退出,不会解开堆栈。 (因为pthreads规范是用C语言而不是C ++定义的,所以它没有定义C ++析构函数发生了什么,所以它的平台特定)。
$ b $
因为这种方式,向量的析构函数在你调用 pthread_exit()
。
如何确保在退出pthread时堆栈变量(以及它们在容器方面的内容)被正确清除? / p>
只需从线程函数返回,就不需要使用 pthread_exit
退出线程启动函数(传递给 pthread_create
的函数)。 POSIX 说:
main()的线程之外的线程时,隐式调用 pthread_exit()
和 GNU / Linux手册页也有同样的说法:
执行从
以外的任何线程的start函数返回主线程导致隐式调用 pthread_exit ()
,使用
函数的返回值作为线程的退出状态。
使用 pthread_exit
从堆栈中的其他函数退出线程,就像可以使用 exit()
退出一个程序而不是从 main()
返回,但在最外层的函数只是 return NULL;
使用 pthread_exit(x)
的唯一时间只是简单地 return x; 位于 main
,它会导致程序等待其他线程完成。
I have noticed that when I call a method using a thread in the form
////main code/////
pthread_t thread;
pthread_create(thread,function,data);
//////////////////
void* function(void* data){
//work with some data on the heap via a vector on the stack
std::vector<double> variable (100,1.2345);
//do contents of thread
pthread_exit(NULL);
}
despite having no calls to new
(except implicitly in vector variable) I get a memory leak with the amount of memory usage going linearly with the number of times I call function
in this way.
However if I do this
void* function(void* data){
{
std::vector<double> variable (100,1.2345);
//do contents of thread
}
pthread_exit(NULL);
}
the memory leak doesn't occur.
It seems that pthread_exit(NULL)
doesn't clear the stack variables like you would get at the end of a normal function with return
(I am correct about that right?!) so putting them within their own scope ensures they get freed.
However, this seems like a massive kludge. How do I ensure the stack variables (and their contents on the heap in terms of containers) are cleared properly when exiting a pthread?
解决方案
It seems that pthread_exit(NULL)
doesn't clear the stack variables like you would get at the end of a normal function with return (I am correct about that right?!)
That's like calling exit(0)
in non-threaded code, the program exits right away and doesn't unwind the stack. (Since the pthreads spec is defined in terms of C, not C++, it doesn't define what happens with C++ destructors, so it's platform specific).
so putting them within their own scope ensures they get freed.
Because that way the vector's destructor runs before you call pthread_exit()
.
How do I ensure the stack variables (and their contents on the heap in terms of containers) are cleared properly when exiting a pthread?
Just return from the thread function, you don't need to use pthread_exit
to exit from the thread start function (the one that was passed to pthread_create
). POSIX says:
An implicit call to pthread_exit()
is made when a thread other than the thread in which main()
was first invoked returns from the start routine that was used to create it.
and the GNU/Linux man page says the same thing slightly differently:
Performing a return from the start function of any thread other than
the main thread results in an implicit call to pthread_exit()
, using
the function's return value as the thread's exit status.
You can use pthread_exit
to exit the thread from other functions further down the stack, just like you can use exit()
to quit a program instead of returning from main()
, but at the outermost function just return NULL;
(or whatever return value you want).
The only time using pthread_exit(x)
makes a difference to simply return x;
is in main
where it will cause the program to wait until other threads finish.
这篇关于pthread内存泄漏与堆栈变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!