C++ deleting pointers from vector causes heap error -


i have vector of pointers objects of base class , store objects derived class in it. way add pointers quite obvious

std::vector<element*> mvgates; ... mvgates.push_back(new node(x, y)); 

and when program closed pointers freed in 'for' loop

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

at line debugger (i'm using qt creator) showing few lines that:

heap block @ 14e50f50 modified @ 14e50f84 past requested size of 2c invalid address specified rtlfreeheap( 00030000, 14e50f58 ) 

what can cause, doing wrong? know haven't showed full code, since it's quite long, maybe enough tell me mistake. recommend me using smart pointers. think doesn't answer question - can wrong code.

edit: simple mistake, couldn't have noticed here. in node class i've declared some_type array[0];

and i've been using 2 elements array. don't know why didn't cause sigsegv thing caused heap error.

your std::vector contains pointers element class instances filling using node class instances. since compiler not complain can safely assume node extends element.

the problem when delete these elements referring them elements. unless overriding correctly element destructor delete operation call element::~element() , not node::~node()

in class definition make sure destructor virtual:

class element{    virtual ~element(){       //element cleanup    }     //.... }; 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -