c++ vector problems -
i working std::vector
hold objects have dynamically allocated members, , when go put things vector few things happen not understanding.
i call
push_back()
, use constructor of objects argument, reason goes destructor of object. why this; should adding not deleting?i call
push_back()
second time doing same before, time throws illegal memory access @dbgdel.cpp
operator delete (line 52). delete should never called in constructor, orpush_back()
.
i uncertain sections of code pertinent question lines in question pretty entrenched in method.
edit: code added
class thing{ int** array; int size; // of square array point current; // location thing(int _n){ // allocates, , gives values array, , members // constructor } }; class thingmgr{ thing * control; thing * current; thing * previous; int size; // size of all. same use in thing thingmgr(int _n){ size = _n; control = new thing(size); current = new thing(size); previous = new thing(size); } void rearrange(int _num){ std::vector<thing> possibles; // performs deterministic work on members // [0] first possibles.push_back(thing(size)); // succeeds // [1] second possibles.push_back(thing(size)); // fails // more operations performed never reached. } };
first: call push_back() , use constructor of objects argument, reason goes destructor of object. why this; should adding not deleting?
you pushing copy of element vector
. construct temporal element, copy-constructor called create copy within vector
, destructor of temporal element called.
second: call push_back() second time doing same before, time throws illegal memory access @ dbgdel.cpp operator delete (line 52). delete should never called in constructor, or push_back().
it's strange happen on second call, when vector
needs regrow copies elements again.
you failing provide proper copy-constructor element in question.
Comments
Post a Comment