简体   繁体   English

指向对象的指针向量-内存泄漏

[英]Vector of pointers to Objects - Memory leaks

I have a vector of pointers to class objects. 我有一个指向类对象的指针向量。 These class objects invoke "new" as well to create an array. 这些类对象也调用“新”来创建数组。

I'm trying to avoid memory leaks, so I created a destructor which returns the object's array back to freestore. 我试图避免内存泄漏,所以我创建了一个析构函数,该析构函数将对象的数组返回给freestore。

laboratory::~laboratory()
{
   delete Users;   // Users is an array from the heap
}

When I try to delete each element of the pointer vector, however, the program crashes: 但是,当我尝试删除指针向量的每个元素时,程序崩溃:

for(int i = 0; i < vectorSize; i++)
    delete labVector[i];

Any help is much appreciated. 任何帮助深表感谢。

Edit: Pasted code here: http://pastie.org/4168453 编辑:此处粘贴代码: http : //pastie.org/4168453

Class and function definitions are below main(). 类和函数的定义在main()下面。 Sorry for pasting it this way, I was using a header file and 2 source files. 很抱歉以这种方式粘贴,我正在使用头文件和2个源文件。

delete Users;   // Users is an array from the heap

Well then that's wrong. 那么那是错误的。 Should be 应该

delete [] Users;

Anything you new with [] gets a delete with [] . 任何你new[]获取delete[]

On a side note; 附带说明; do you really need a vector of pointers here? 真的需要这里的指针向量吗? It's pretty darn rare to actually need that (though you see it a lot) and it completely negates the container's ability to manage memory for you, requiring you to loop through and deallocate every element. 真正需要它的确很少(尽管您经常看到),并且它完全否定了容器为您管理内存的能力,需要您遍历并重新分配每个元素。

vectors use dynamic memory behind the scenes for each element. 向量为每个元素在后台使用动态内存。 You could us a vector of smart pointers or even a vector<vector<T>> (though, if performance is of the utmost concern, a jagged array may be a better choice. Don't assume that though). 您可以给我们一个智能指针的向量,或者甚至是一个vector<vector<T>> (但是,如果性能是最重要的问题,则锯齿状的数组可能是一个更好的选择。尽管如此,请不要假设)。

除非您绝对需要自己执行此操作,否则请考虑使用Boost ptr_vector

You have a few choices: 您有几种选择:

  1. You can make it clear that the vector does not own the objects in it, only the pointers to them. 您可以清楚地知道,向量不拥有其中的对象,而仅拥有指向它们的指针。 It then becomes the responsibility of all code that uses this vector to allocate and delete the objects itself. 然后,所有使用此向量分配和删除对象本身的代码都将成为责任。

  2. You can use a boost ptr_vector for this. 您可以为此使用boost ptr_vector The vector will own the objects it contains pointers to. 该向量将拥有其包含指针的对象。 You'll need to write a helper function to duplicate the objects so that x = y; 您需要编写一个辅助函数来复制对象,以便x = y; will work. 将工作。 (Since x will need to own its own copy of every object in y .) (因为x需要拥有y中每个对象的副本)。

  3. You can use smart pointers, such as boost::shared_ptr , for this. 您可以为此使用智能指针,例如boost::shared_ptr If you do x = y; 如果x = y; , the two vectors will refer to the same objects, such that changing the value in one will change the value in the other. ,这两个向量将引用相同的对象,因此,更改一个向量的值将更改另一个向量的值。 The vectors will share ownership of the objects and can return safe references to the objects in them. 向量将共享对象的所有权,并可以返回对其中对象的安全引用。 The objects will self-destruct when they are no longer needed. 当不再需要这些对象时,它们将自毁。

  4. You can use a vector of boost::any objects. 您可以使用boost::any对象的向量。 This performs poorly but is very flexible. 这表现不佳,但非常灵活。

But it really comes down to the classic question: What are you trying to do? 但这确实归结为经典问题: 您要做什么? Are you trying to manage the lifetimes of objects? 您是否要管理对象的生存期? If you duplicate a vector, what should happen? 如果复制向量,应该怎么办? Should that duplicate the underlying objects? 那应该复制底层对象吗? Do you need polymorphism? 您需要多态吗? Are you using pointers to avoid slicing? 您是否使用指针来避免切片? And so on. 等等。 Explain your use case and you'll get better solution suggestions. 解释您的用例,您将获得更好的解决方案建议。

Update : Your use of a vector of pointers seems simple and safe (so long as you don't try to copy-construct a vector, assign it, or anything like that). 更新 :使用指针向量似乎简单又安全(只要您不尝试复制构造向量,分配向量或类似操作)。 That your delete loop is crashing suggests possibly a bug in your destructor. 您的delete循环崩溃表明,析构函数中可能存在错误。

Update2 : Yep. Update2 :是的。

laboratory::~laboratory()
{
    delete stationUsers;
}

This is wrong because stationUsers wasn't allocated with new but with new[] . 这是错误的,因为stationUsers并未分配new而是分配了new[]

没有更多代码很难知道,但请考虑:

delete[] Users;

Do not allocate an array from the heap yourself. 不要自己从堆中分配数组。 Use std::vector<User> . 使用std::vector<User> In addition, you should never, ever use delete or delete[] yourself. 另外,您永远都不要自己使用deletedelete[] Always use a class-based solution such as smart pointers or std::vector to manage your memory. 始终使用基于类的解决方案,例如智能指针或std::vector来管理您的内存。

In addition to the others comments, your code has an Off-by-One error. 除了其他注释之外,您的代码还有一个“一站式”错误。 This code references one past the end of your array. 此代码引用了数组末尾的一个。

for(int i = 0; i `<` vectorSize; i++)
    delete labVector[i];

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

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