0

我有一个指向类对象的指针向量。这些类对象也调用“new”来创建一个数组。

我试图避免内存泄漏,所以我创建了一个析构函数,它将对象的数组返回给 freestore。

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

但是,当我尝试删除指针向量的每个元素时,程序崩溃:

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

任何帮助深表感谢。

编辑:在此处粘贴代码:http: //pastie.org/4168453

类和函数定义在 main() 下面。很抱歉以这种方式粘贴它,我使用的是一个头文件和 2 个源文件。

4

6 回答 6

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

那么那是错误的。应该

delete [] Users;

任何你new拥有的东西都会[]得到一个deletewith []

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).

于 2012-06-28T23:35:27.720 回答
3

Unless you absolutely need to do this on your own, consider using a Boost ptr_vector instead.

于 2012-06-28T23:38:45.280 回答
3

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. 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; will work. (Since x will need to own its own copy of every object in y.)

  3. You can use smart pointers, such as boost::shared_ptr, for this. If you do 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. 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.

Update2: Yep.

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

This is wrong because stationUsers wasn't allocated with new but with new[].

于 2012-06-28T23:43:36.197 回答
1

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

delete[] Users;
于 2012-06-28T23:32:21.167 回答
0

Do not allocate an array from the heap yourself. Use std::vector<User>. In addition, you should never, ever use delete or delete[] yourself. Always use a class-based solution such as smart pointers or std::vector to manage your memory.

于 2012-06-28T23:37:37.267 回答
0

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];
于 2012-06-28T23:43:13.010 回答