I’ve defined a C++ class with the following header file:
class EarleyParser
{
public:
EarleyParser();
virtual ~EarleyParser();
void initialize( string filePath, bool probabilityParse );
private:
bool probabilityParser;
typedef unordered_map< string, list<Production>* > productionHashTable;
productionHashTable earlyHashTable;
};
As you can see a member element of the class is an unordered_map whose key element is a string and content element is a pointer to a list of objects of another class named Production (don’t mind it, it could be anything).
My question is if I should leave it to the default destructor to free memory allocated, or if I should manually inspect the hash table and delete all of its elements.
In the second case what would be the procedure? Calling this for each element will be ok?
EarleyParser::productionHashTable::const_iterator got = this->earlyHashTable.find( "key" );
delete[] got->second;
You need to clarify who owns the
list<Production>objects owned byEarlyParser. IfEarlyParserowns them, then you need to free the resources. You can do it by iterating over the list and callingdeleteon each dereferenced iterator (notdelete[]). Or you can storeunique_ptr<list<Production>>instead. On the other hand, the simplest solution is to storelist<Production>unless you really have very strong reasons for storing pointers.