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..
haven't gone through all of your code but from the first few lines it seems you're maintaining sets of pointers. Whenever you have an STL container which holds pointers and you're using
new
to put stuff in the pointers, you must use delete to deallocate these pointers. STL doesn't do that for you. In fact, STL doesn't even know they are pointers.Another option is to not use pointers at all and have a set of just the objects and not use
new
to create them. Just create them on the stack and copy them into the set.