47 memory leaks. STL pointers.
- by icelated
I have a major amount of memory leaks. I know that the Sets have pointers and i cannot change that! I cannot change anything, but clean up the mess i have...
I am creating memory with new in just about every function to add information to the sets.
I have a Cd/ DVD/book: super classes of ITEM class and a library class..
In the library class i have 2 functions for cleaning up the sets.. Also, the CD, DVD, book destructors are not being called..
here is my potential leaks..
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();
void Purge(ItemSet &set);
void Purge(ItemSetMap &map);
};
here is some functions for adding info using new in library. Keep in mind i am cutting out alot of code to keep this post short.
library.cpp
#include "Library.h"
#include "book.h"
#include "cd.h"
#include "dvd.h"
#include <iostream>
// general functions
ItemSet allBooks;
ItemSet allCDS;
ItemSet allDVDs;
ItemSetMap allBooksByAuthor;
ItemSetMap allmoviesByDirector;
ItemSetMap allmoviesByActor;
ItemSetMap allMusicByBand;
ItemSetMap allMusicByMusician;
const ItemSet* Library::itemsForKeyword(const string& keyword) const
{
const StringSet* kw;
ItemSet* obj = new ItemSet();
return obj;
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);
return item;
const Item* Library::addMusicCD(const string& title, const string& band, const int nSongs)
{
ItemSet* obj = new ItemSet();
CD* item = new CD(title,band,nSongs);
return item;
void Library::addBandMember(const Item* musicCD, const string& member)
{
ItemSet* obj = new ItemSet();
(((CD*) musicCD)->addBandMember(member));
obj->insert((CD*) musicCD);
here is the library destructor.....
Library::~Library()
{
Purge(allBooks);
Purge(allCDS);
Purge(allDVDs);
Purge(allBooksByAuthor);
Purge(allmoviesByDirector);
Purge(allmoviesByActor);
Purge(allMusicByBand);
Purge(allMusicByMusician);
}
void Library::Purge(ItemSet &set)
{
for (ItemSet::iterator it = set.begin(); it != set.end(); ++it)
delete *it;
set.clear();
}
void Library::Purge(ItemSetMap &map)
{
for (ItemSetMap::iterator it = map.begin(); it != map.end(); ++it)
delete it->second;
map.clear();
}
so, basically item, cd, dvd class all have a set like this:
typedef set<string> StringSet;
class CD : public Item
StringSet* music;
and i am deleting it like: but those superclasses are not being called.. Item destructor is.
CD::~CD()
{
delete music;
}
Do, i need a copy constructor? and how do i delete those objects i am creating in the library class? and how can i get the cd,dvd, destructor called?
would the addbandmember function located in the library.cpp cause me to have a copy constructor?
Any real help you can provide me to help me clean up this mess instead of telling me not to use pointers in my sets i would really appreciate. How can i delete the memory i am creating in those functions? I cannot delete them in the function!!