Search code examples
c++destructormultimap

How Do I Deal With This C++ Multimap Destructor?


I am a beginner in C++ working on simple program and I ran into an issue which left me stumped... I do not understand where the memory is leaking. I understand more precisely, but I cannot write the destructor correctly. I ask for help.

Here is the class DBMAP

class DBMAP
{
    private:
        multimap < int, Volkov_1* > setMAP;
    public:
        DBMAP() {};
        DBMAP(DB& db)(DB & db)
        {
            db.copyM(*this);
        };
        void addM(Volkov_1* b)
        {
            setMAP.insert(pair<int, Volkov_1*>(*b->x, b));
        };
        ~DBMAP()
        //for (multimap< int, Volkov_1* >::iterator it = setMAP.begin(); it != setMAP.end(); ++it)
        // delete* it;;??

};

Solution

  • ~DBMAP()
    {
      for (multimap< int, Volkov_1* >::iterator it = setMAP.begin(); it != setMAP.end(); ++it)
          delete it->second;
    }
    

    A multimap iterator points to a pair of values. The first of the pair is the key, the second is the value. That's the pointer you want to delete.