memory management - C++ Unexpected error when deleting objects -


i write interpreter, in each keyword, syntax notation or operator has base class of token.

class token {     private:         static std::vector<token *> registered;          size_t id;          std::string name;         std::string symbol;      public:         token(const std::string& name, const std::string& symbol);         token::~token();          token(const token& rhs) = delete;         token& operator =(const token& rhs) = delete;          /* ... */          static void deleteregistered(); }; 

the constructor:

token::token(const std::string& name, const std::string& symbol)                 : name(name), symbol(symbol) {     token::registered.push_back(this);     this->id = token::registered.size(); } 

the destructor:

token::~token() {     // removes 'this' token::registered     token::registered.erase(std::remove(token::registered.begin(), token::registered.end(), this), token::registered.end()); } 

deleteregistered:

void token::deleteregistered() {     (size_t = 0; < token::registered.size(); ++i) {         delete token::registered[i];     } } 

in code, many different classes store containers of pointers sub-classes derive token.

in order avoid deleting objects twice or more, store references of allocated instances, , have static method delete them all.

the method deleteregistered called after operations done executing.

now, problem:

when call token::deleteregistered (which happens few lines before program exits, fails , in debug shows following:

file: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp line: 52  expression: _block_type_is_valid(phead->nblockuse) 

the memory, 1 line before programs crashes

since of token instances don't have defined scope, came design, me seems ok.

what can cause error?

edit: destructor late addition of mine, commenting out still shows error above. delete fails delete first item in container.

2nd edit: example of how use token:

this->parser.operators.add(new refbinaryoperator(     "assignment", "=", 14, refbinaryoperator::assign )); 

note: refbinaryoperator sub-class of token (not direct), calls token's constructor.

so instance, pull pointers tokens operators container, , assign them other structures. when done, call deleteregistered.

final edit:

i got working declaring token destructor virtual:

does delete work pointers base class?

what happening in token::~token() calling erase registered vector invalidating indices used in loop noticed problem. need remember once call erase on vector, loop indices need adjusted properly. if keep current destructor following work in deleteregistered:

void deleteregistered() {   while(!token::registered.empty())     delete *token::registered.begin(); } 

also, since have many classes extend off of token, ~token() should become virtual destruction base class handled.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -