简体   繁体   English

C ++从向量中删除指针会导致堆错误

[英]C++ deleting pointers from vector causes heap error

I have a vector of pointers to objects of my base class and then I store objects from derived class in it. 我有一个指向我的基类对象的指针向量,然后将派生类中的对象存储在其中。 The way I add pointers is quite obvious 我添加指针的方式非常明显

std::vector<Element*> mvGates;
...
mvGates.push_back(new Node(x, y));

And when my program is about to be closed those pointers are freed in 'for' loop 当我的程序即将关闭时,这些指针将在“ for”循环中释放

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

At this line my debugger (I'm using Qt Creator) is showing a few lines like that: 在这一行,我的调试器(我正在使用Qt Creator)显示了如下几行:

Heap block at 14E50F50 modified at 14E50F84 past requested size of 2c
Invalid address specified to RtlFreeHeap( 00030000, 14E50F58 )

What can be the cause, what am I doing wrong? 原因可能是什么,我做错了什么? I know that I haven't showed you full code, since it's quite long, but maybe it is enough to tell me my mistake. 我知道我没有显示完整的代码,因为它很长,但是也许足以告诉我我的错误。 Probably you will recommend me using smart pointers. 可能您会推荐我使用智能指针。 I think I will do it but that doesn't answer my question - what can be wrong with this code. 我想我会做的,但是并不能回答我的问题-此代码可能有什么问题。

EDIT: It was a very simple mistake, you couldn't have noticed it here. 编辑:这是一个非常简单的错误,您在这里可能没有注意到。 In Node class I've declared some_type array[0]; 在Node类中,我声明了some_type array[0];

And I've been using it as a 2 elements array. 我一直在使用它作为2个元素的数组。 I don't know why it didn't cause a SIGSEGV but it was the thing that caused heap error. 我不知道为什么它没有导致SIGSEGV,但是是导致堆错误的原因。

Your std::vector contains pointers to Element class instances but your are filling it using Node class instances. 您的std :: vector包含指向Element类实例的指针,但是您正在使用Node类实例填充它。 Since the compiler does not complain we can safely assume that Node extends Element. 由于编译器没有抱怨,我们可以安全地假定Node扩展了Element。

The problem is that when you delete these elements you are referring to them as Elements. 问题在于,当删除这些元素时,会将它们称为元素。 Unless you are overriding correctly Element destructor the delete operation will call Element::~Element() and not Node::~Node() 除非正确覆盖了元素析构函数,否则删除操作将调用Element ::〜Element()而不是Node ::〜Node()

In your class definition make sure that the destructor is virtual: 在类定义中,确保析构函数是虚拟的:

class Element{
   virtual ~Element(){
      //Element cleanup
   }

   //....
};

In your code, your are visiting each object in vector by using index, and you are deleting them. 在您的代码中,您正在通过使用索引访问vector中的每个对象,并且正在删除它们。 However, besides what "Pierluigi" said (the virtual ~ problem), shouldn't you visit each object and delete them by using iterator rather than by using index directly? 但是,除了“ Pierluigi”所说的(虚拟〜问题)之外,您是否不应该使用迭代器而不是直接使用索引访问每个对象并删除它们?

You had mistaken in the incremental part. 您在增量部分中弄错了。 Its i++ you need not ++i. 它的i ++不需要++ i。

If you use ++i, it will increment before looping into for loop so at the iterator number it is referring mvGates[i] which actually doesn't exist. 如果使用++ i,它将在循环到for循环之前增加,因此在迭代器编号处,它引用的是mvGates [i],而实际上这是不存在的。 Only vector ranges from 0 to length-1. 仅矢量范围从0到length-1。

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

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

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