循环中的std :: unique_ptr - 内存泄漏 [英] std::unique_ptr in a loop - memory leaks
问题描述
unique_ptr是否按照预期正确使用?代码以一些内存泄漏(可能是误报?或真正的泄漏?)结束。我认为将所有权移到RunSimulation中,那么unique_ptr的生命将会结束,而新的将在循环中创建,然而,这会以访问冲突结束。
理想情况下,想要main()中的unique_ptr对象之一在main()中到期,另一个在main之外发送,以便在RunSimulation()中释放。
class结果{public:int n; };
void RunSimulation(std :: unique_ptr< Result> result){result-> n = 0;}
void main()
{
boost :: thread_group线程;
std :: unique_ptr<结果> R等
std :: unique_ptr<模拟> sim = std :: make_unique< Simulation>();
for(int i = 0; i <10; i ++)
{
r = std :: unique_ptr< Result>(new Result);
//错误的行:
//threads.create_thread(boost:runSimulation,std :: move(r)));
//threads.create_thread([&] {RunSimulation(std :: move(r));});
}
threads.join_all();
您没有内存泄漏,但这并不是说你的代码是正确的。看看你的循环:
$ p $ for(int i = 0; i <10; i ++)
{
r = std :: unique_ptr< Result>(new Result);
threads.create_thread([&] {RunSimulation(r.get());});
$ b每次你给r分配一个新的Result实例,旧的被删除由unique_ptr。这可能发生在它的指针使用它的线程之前,在这种情况下,你试图解引用已删除的内存。这是未定义的行为。
更新
下面是一个例子
#include< thread>
#include< memory>
#include< iostream>
类结果
{
public:
Result():x(0){}
int x;
};
void RunSimulation(std :: unique_ptr< Result> result)
{
result-> x = 10;
std :: cout<<结果 - > x<<的std :: ENDL;
$ b int main()
{
std :: unique_ptr< Result> result = std :: make_unique< Result>();
std :: thread t(RunSimulation,std :: move(result));
t.join();
}
Is unique_ptr being properly used below as intended? The code ends with some memory leaks (possibly false positives? or real leaks?). I thought moving the ownership to RunSimulation where life of unique_ptr would end and newer ones would be created in the loop, however, that ends in an access violation.
Ideally, I would like one of the unique_ptr objects in main() to expire within main() and the other one sent outside main to free up in RunSimulation().
class Result { public: int n; };
void RunSimulation(std::unique_ptr<Result> result) {result->n = 0;}
void main()
{
boost::thread_group threads;
std::unique_ptr<Result> r;
std::unique_ptr<Simulation> sim = std::make_unique<Simulation>();
for (int i = 0; i < 10; i++)
{
r = std::unique_ptr<Result>(new Result);
//Erroneous lines:
//threads.create_thread(boost::bind(&RunSimulation, std::move(r)));
//threads.create_thread([&] {RunSimulation(std::move(r)); });
}
threads.join_all();
}
You don't have a memory leak, but that's not to say that your code is correct. Take a look at your loop:
for (int i = 0; i < 10; i++)
{
r = std::unique_ptr<Result>(new Result);
threads.create_thread([&] { RunSimulation(r.get()); });
}
Every time you assign a new Result instance to r, the old one gets deleted by the unique_ptr. This could happen before the thread that takes its pointer uses it, in which case you are trying to dereference deleted memory. This is undefined behaviour.
Update Here's an example
#include <thread>
#include <memory>
#include <iostream>
class Result
{
public:
Result() : x(0) { }
int x;
};
void RunSimulation(std::unique_ptr<Result> result)
{
result->x = 10;
std::cout << result->x << std::endl;
}
int main()
{
std::unique_ptr<Result> result = std::make_unique<Result>();
std::thread t(RunSimulation, std::move(result));
t.join();
}
这篇关于循环中的std :: unique_ptr - 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!