Memory Leaks - STL sets

c++, memory-leaks, set, stl

Solution

You need to free the memory for each element of the set. The container will not do that for you, and it shouldn't because it can't know whether it owns that data or not -- it could just be holding pointers to objects owned by something else.

This is a generic free function that will deallocate any STL container.

template <typename T>
void deallocate_container(T& c)
{
  for (typename T::iterator i = c.begin(); i != c.end(); ++i)
    delete *i;
}

// Usage
set<SomeType*> my_set;
deallocate_container(my_set);
my_set.clear();

Problem

I am trying to plug up all my memory leaks (which is massive). I am new to STL. I have a class library where I have 3 sets. I am also creating a lot of memory with new in the library class for adding info to the sets... Do I need to deallocate the sets? If so, how? Here is the library.h ``` #pragma once #include <ostream> #include <map> #include <set> #include <string> #include "Item.h" using namespace std; typedef set<Item*> ItemSet; typedef map<string,Item*> ItemMap; typedef map<string,ItemSet*> ItemSetMap; class Library { public: // general functions void addKeywordForItem(const Item* const item, const string& keyword); const ItemSet* itemsForKeyword(const string& keyword) const; void printItem(ostream& out, const Item* const item) const; // book-related functions const Item* addBook(const string& title, const string& author, int const nPages); const ItemSet* booksByAuthor(const string& author) const; const ItemSet* books() const; // music-related functions const Item* addMusicCD(const string& title, const string& band, const int nSongs); void addBandMember(const Item* const musicCD, const string& member); const ItemSet* musicByBand(const string& band) const; const ItemSet* musicByMusician(const string& musician) const; const ItemSet* musicCDs() const; // movie-related functions const Item* addMovieDVD(const string& title, const string& director, const int nScenes); void addCastMember(const Item* const movie, const string& member); const ItemSet* moviesByDirector(const string& director) const; const ItemSet* moviesByActor(const string& actor) const; const ItemSet* movies() const; ~Library(); }; ``` I am not sure what i need to do for the destructor? ``` Library::~Library() { } ``` also, am I de allocating the stringset right? ``` #ifndef CD_H #define CD_H #pragma once #include "item.h" #include <set> typedef set<string> StringSet; class CD : public Item { public: CD(const string& theTitle, const string& theBand, const int snumber); void addBandMember(const string& member); const int getNumber() const; const StringSet* getMusician() const; const string getBand() const; virtual void print(ostream& out) const; string printmusicians(const StringSet* musicians) const; ~CD(); private: string band; StringSet* music; string title; int number; }; ostream& operator<<(ostream& out, const CD* cd); #endif ``` cd.cpp ``` #include "CD.h" using namespace std; CD::CD(const string& theTitle, const string& theBand, const int snumber) : Item(theTitle), band(theBand),number(snumber), music(new StringSet) { } CD::~CD() { delete []music; } ``` in the library class I am creating a lot of memory, but dont the destructor clean that up? example: ``` const Item* Library::addBook(const string& title, const string& author, const int nPages) { ItemSet* obj = new ItemSet(); Book* item = new Book(title,author,nPages); allBooks.insert(item); // add to set of all books obj->insert(item); ``` Note: I do not have a copy constructor. I am not sure if I even need one or how top add one. I dont think my destructors are getting called either..

Original source

Related problems