I write an interpreter, in which each keyword, syntax notation or operator has the 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' from Token::registered
Token::registered.erase(std::remove(Token::registered.begin(), Token::registered.end(), this), Token::registered.end());
}
DeleteRegistered:
void Token::DeleteRegistered() {
for (size_t i = 0; i < Token::registered.size(); ++i) {
delete Token::registered[i];
}
}
In my code, many different classes store containers of pointers to sub-classes which eventually derive from Token.
In order to avoid deleting objects twice or more, I store references to all of the allocated instances, and have a static method which will delete them all.
The method DeleteRegistered is called after all operations are done executing.
Now, to my problem:
When I call Token::DeleteRegistered (which happens a few lines before the program exits, it fails and in debug shows the following:
File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp
Line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Since all of the Token instances don't really have a defined scope, I came up with this design, which to me currently seems OK.
What can cause this error?
EDIT:
The destructor was a late addition of mine, commenting it out still shows the error above. The delete fails to delete even the first item in the container.
2ND EDIT:
An example of how I use Token:
this->parser.Operators.Add(new RefBinaryOperator(
"Assignment", "=", 14, RefBinaryOperator::Assign
));
Note: RefBinaryOperator is a sub-class of Token (Not direct), which eventually calls Token's constructor.
So for instance, I pull pointers to Tokens from the Operators container, and assign them to other structures. When everything is done, I call DeleteRegistered.
FINAL EDIT:
I got it working by declaring the Token destructor as virtual:
Tokenlifetime you might end up deleting tokens defined on the Stack or inside another class. Also , how to you create the tokens ?Token::DeleteRegistered()won't delete most of the objects, but I don't see how that would cause a crash. Are you perhaps using theidmember for anything? Once you delete an object, theids stop matching and you will be getting multiple instances with same id...new; how are you making sure all of them are deletable?In order to avoid deleting objects twice or more, I store references to all of the allocated instances,Maybe you should invest in using a smart pointer such as std::shared_ptr instead of trying to keep track of number of instances. Using shared_ptr, once remove() does its job, that's it. You don't need that static member array.new; which is one possible cause of failure.