Hi,

I define a class which has a std::map static data member that i use it to store a limited amount of instances (the constructor is private). A static class method TheClass::instance(map_index) returns a reference to the instance corresponding to the supplied map_index. The map is filled at runtime, when the static method TheClass::instance is used for the first time. (The instances contain data loaded from files thus i use this technique so that the data is loaded only once).

My question is this: since i fill the map dynamically at runtime (using new), at what point do i actually free it (using delete)? This means i'd have a map of raw pointers. Would i be able to instead have a map of instances or smart pointers? I can't see where the memory of the instances would be (can't be on the stack, and if it's on the heap, at some point i need to free it), and the smart pointer itself would have the same problem.

Here is the definition of the method which fills the map (simplified for clarity):
Code:
void Sector::getDefinedSectors() {
  DIR *pdir;
  struct dirent *pent;

  pdir=opendir(dataPath);

  while((pent = readdir(pdir))) {
    std::string filename = dataPath + std::string(pent->d_name);
    if(fileIsModel(filename)) {
      Sector* s = new Sector(filename);
      const Secteur::Openings& openings = s.openings();
      definedSectors.insert(std::map<openings, *Sector>::value_type(openings, s));
    }
  }

}
In fact, I don't even see when i'd actually iterate through the map to delete the instances! I'd have to have a static cleanup method to manually free the map... ugh. Is there a more robust way to do this?