简体   繁体   English

内存泄漏,向量推回 C++

[英]Memory leak, vector pushback c++

I have a class Bar , its constructor initializes a std::vector of type Foo (another class).我有一个类Bar ,它的构造函数初始化了一个Foo类型的std::vector (另一个类)。

Bar.cpp命令行工具

Bar::Bar(int n) {
 for(int i = 0; i < n; i++) {
    Foo foo;
    foo.somefunction(i);
    vec.push_back(foo) //this should insert foo into the vector
  }
}

Bar.h酒吧.h

class Foo;
class Bar {
 std::vector<Foo> vec;
};

When I debug, the first iteration of the construction works fine.当我调试时,构造的第一次迭代工作正常。 foo is created, foo.somefunction() is run fine, and foo is pushed into vec . foo被创建, foo.somefunction()运行良好, foo被推入vec

The second interation seems to work fine as well, but the program crashes when it goes back to start the third iteration.第二次迭代似乎也能正常工作,但是当它返回开始第三次迭代时程序崩溃了。

I get _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) error and HEAP CORRUPTION DETECTED .我收到_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)错误和HEAP CORRUPTION DETECTED _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) HEAP CORRUPTION DETECTED

Foo is a class that contains an dynamically created array, nothing special. Foo是一个包含动态创建的数组的类,没什么特别的。 Something like this:像这样的东西:

Foo::Foo() {
  solution = new float[size];
  // some function that initializes the elements of solution
}

and a regular destructor ~Foo() {delete [] solution;} .和一个常规的析构函数~Foo() {delete [] solution;} I don't think the problem comes from Foo .我不认为问题来自Foo

Most likely you did not implement the copy constructor and operator = properly and you are double deleting solution .很可能您没有正确实现copy constructoroperator =并且您正在重复删除solution As mentioned you should also read up on The Rule of Three .如前所述,您还应该阅读“三法则”

The C++ standard containers store copies and so when you do a push_back you are making a copy. C++ 标准容器存储副本,因此当您执行push_back您正在制作副本。

It looks like you didn't implement copy constructor in class Foo.看起来您没有在类 Foo 中实现复制构造函数。 In Bar constructor each iteration new instance of Foo is created and destroyed when the iteration is over.在 Bar 构造函数中,每次迭代都会在迭代结束时创建和销毁 Foo 的新实例。 So the memory allocated in Foo is destroyed, but default copy constructor that copied instance of Foo to vector didn't copy the memory you allocate with "new", just copied the pointer.所以在 Foo 中分配的内存被破坏了,但是将 Foo 的实例复制到 vector 的默认复制构造函数没有复制你用“new”分配的内存,只是复制了指针。 So after each iteration each vector element is corrupted.所以每次迭代后,每个向量元素都会被破坏。 Let assume that vector had some allocated memory for your objects at the beginning.假设 vector 在开始时为您的对象分配了一些内存。 Then when there was no place in the buffer and he couldn't grow that buffer any more, vector allocates new memory and another copy operation happens.然后,当缓冲区中没有位置并且他无法再增加该缓冲区时,vector 会分配新内存并发生另一个复制操作。 When copy operation is finished old buffer need to be freed and vector destroys all objects in old buffer and destructor in each object will call delete[] on corrupted pointer.复制操作完成后,需要释放旧缓冲区,向量销毁旧缓冲区中的所有对象,每个对象中的析构函数将对损坏的指针调用 delete[]。

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

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