简体   繁体   English

从指针向量的向量中释放内存

[英]Deallocating memory from a vector of vectors of pointers

I'm creating a particle physics simulator and I need to make proper memory management. 我正在创建一个粒子物理模拟器,我需要进行适当的内存管理。

I've found convenient that my method propagates several particles at once so this method returns a vector of trajectories and each trajectory is a vector of Steps (thus getting a vector< vector<> > ). 我发现我的方法一次传播多个粒子很方便,因此该方法返回轨迹的向量,每个轨迹都是Steps的向量(因此得到vector< vector<> > )。

(Step is a class I created.) (Step是我创建的一个类。)

vector< vector<Step*> > Propagator::Propagate (vector<TParticle*> vpart) {
  vector< vector<Step*> > = vvsteps;
  //vvsteps goes through physics and gets filled using push_back
  //all of vvsteps' entries were filled with objects created with "new"

  return vvsteps;
}

Each Step creates a vector of pointers to TParticle (created with new) and has the following destructor to deallocate it. 每个步骤都会创建一个指向TParticle的指针向量(使用new创建),并具有以下析构函数来对其进行分配。

vector<TParticle*> vpart;

Step::~Step() {
  for(int i=0; i<vpart.size(); i++) delete vpart[i];
  vpart.clear();
}

After I get what I want I try to deallocate the whole thing by doing: 得到我想要的东西后,我尝试通过以下操作来释放整个东西:

vector< vector<Step*> > vvstep = prop->Propagate(vpart);

/*PHYSICS GOES HERE*/

for(int i=0; i<vvstep.size(); i++) {
  for(int j=0; j<vvstep[i].size(); j++)
    delete (vvstep[i])[j];
  vvstep[i].clear();
}
vvstep.clear();

This code for some reason doesn't work. 出于某种原因,该代码无法正常工作。 It gives me the following error 它给我以下错误

*** glibc detected *** bin/rtest.exe: double free or corruption (fasttop): 0x0f7207f0 ***

edit: corrected a typo, the class is named Step and not Steps. 编辑:更正了一个拼写错误,该类名为Step而不是Steps。

Change your vector of vector type to: 将向量类型的向量更改为:

`std::vector< std::vector<std::unique_ptr<Step>>>`

This does a few things. 这会做一些事情。 First, it blocks copying your std::vector s around, which is bad because such vectors represent both ownership and reference to data. 首先,它阻止复制您的std::vector ,这很不好,因为这样的矢量既代表所有权又是对数据的引用。

move is still available, and should generally occur. move仍然可用,通常应该发生。 If you want to move one set of vectors of vectors to another spot, and it isn't happening automatically, insert a std::move( src ) . 如果要将向量的一组向量移动到另一个位置,并且不会自动发生,请插入std::move( src )

Second, when the vector of vector of data goes out of scope, the unique_ptr automatically cleans up the Step objects. 其次,当数据vectorvector超出范围时, unique_ptr自动清除Step对象。

You may have to insert some .get() calls on the unique_ptr<Base> in cases where you are calling a function that takes a Base* directly. 如果要调用直接采用Base*的函数,则可能必须在unique_ptr<Base>上插入一些.get()调用。 But it should otherwise be mostly transparent. 但是否则它应该基本上是透明的。

Note that the double deletion is probably occuring because you have duplicated one of these vector s of Base* -- the std::vector<std::unique_ptr<Base>> will complain when you tried to do that... 请注意,可能发生了重复删除,因为您已经复制了Base*的这些vector之一-尝试这样做时, std::vector<std::unique_ptr<Base>>会抱怨...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM