tags:

views:

278

answers:

3

I have a major amount of memory leaks. I know that the Sets have pointers and i cannot change that! but clean up the mess i have...

I am creating memory with new in just about every function to add information to the sets.

here is my potential leaks..

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;

Do, i need a copy constructor?

+10  A: 

If you pass around pointers, you are responsible for them. Since you refuse to stop using pointers in Sets yourself, one solution would be to use smart pointers.

Brian
+1 use boost shared_ptr
pm100
I suspect even auto_ptr, which is part of standard c++, will be sufficient for this.
Brian
@Brian: Except that STL COAPS (Containers of Auto Pointers) are illegal. (+1 for the answer with smart ptrs though)
Billy ONeal
@BillyONeal: Ah, good to know. Yeah, googling for auto_ptr stl generates plenty of hits warning against using it in STL containers.
Brian
+10  A: 
Jon-Eric
thank you. i made the Item class virtual and now my memory leaks are down to 22
icelated
@icelated: There's a difference between a virtual (base) class and a class with virtual functions. I'm not sure to which your "made the Item class virtual" refers.
sbi
i made the destructor virtual in item and now the cd/ dvd/book destructors are being called- i now only have 22 leaks
icelated
A: 

The thing with pointers is that every time you make a new Anything() then you need to make sure that something will eventually delete() that pointer. Take a step back and think about the life cycle of your new object. Take a look at every time you call new and think to yourself - what class is going to call delete on that pointer?

From the header file it doesn't look like your Library class has any members? One place to start might be to store the pointers from your new()'s there in some fashion, instead of the global?

You make a few calls to ItemSet* obj = new ItemSet(); That's one source of memory leak - the function will go out of scope and you lose the chance to call delete obj forever.

Also make your base class destructors virtual.

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

MauriceL
I understand that i have a memory leak when i do this ItemSet* obj = new ItemSet(); but i cant delete it in that function. So, how do i delete that memory i created??
icelated
Why do you allocate those in the first place if you leak them right away? You allocate a set, insert something in it, and then practically just forget about it (those functions all seem to be missing a closing bracket). - Does your program *work* in the first place?
UncleBens
I'd take a look at what what you're trying to get ItemSet is supposed to do. What you've done is make a new ItemSet, inserted something into it, and then throwing away any way to get at ItemSet (so it becomes useless). Go back, read up and understand how pointers work.
MauriceL