Possible Duplicate:
How to correctly deallocate or delete a c++ vector?
I’m having some problems trying to delete memory that I’ve allocated in a vector. Even though I call list.clear(), it’s not deallocating the memory.
So I have some code like this in a template-based class called Set
template <class T>
class Set {
public:
// stuff
private:
int size;
std::vector<T> list;
};
And in the constructor, I have allocated memory for the vector. So I call list = new std::vector;
For your interest, here is my copy constructor and assignment operator that I’ve also witten where I also allocate memory for the vector:
template <class T>
Set<T>::Set(const Set& aSet)
{
size = aSet.size;
list->clear();
list = new vector<T>;
for (int i = 0; i < size; ++i) {
list[i] = aSet.list[i];
}
}
template <class T>
Set<T>& Set<T>::operator=(const Set& right)
{
if (this != &right) {
list->clear();
size = right.size;
list = new vector<T>;
for (int i = 0; i < size; ++i) {
list[i] = right.list[i];
}
}
return (*this);
}
In the destructor, I just have list.clear() to delete all elements and then deallocate the memory.
But the problem is, when I run valgrind on my .out file, it’s telling me that I’ve definitely lost some memory and I don’t know why it’s telling me this. I’ve read some questions here on Stackoverflow but I’ve basically tried everything. I tried clear() and then delete on the vector but that didn’t work. I then tried to erase(list.begin(), list.end()) but that didn’t work too.
My thought process is that I’m using a Set *aSet = new Set; in my main class and since an int is not an object, it’s not being freed when I call list.clear(). Is this right? How would I go about deleting the memory correctly?
Thanks for any help.
Edit1 = changed list* to setList
My new constructors and the assignment operator:
template <class T>
Set<T>::Set(const Set& aSet)
{
size = aSet.size;
setList.clear();
setList = aSet.setList;
}
template <class T>
Set<T>& Set<T>::operator=(const Set& right)
{
if (this != &right) {
setList.clear();
size = right.size;
setList = right.setList;
}
return (*this);
}
Valgrind still reports I have the same amount of lost memory though. In my destructor I still have list.clear()
Valgrind log:
==11398==
==11398== HEAP SUMMARY:
==11398== in use at exit: 62,969 bytes in 352 blocks
==11398== total heap usage: 540 allocs, 188 frees, 68,046 bytes allocated
==11398==
==11398== LEAK SUMMARY:
==11398== definitely lost: 8,624 bytes in 14 blocks
==11398== indirectly lost: 1,168 bytes in 5 blocks
==11398== possibly lost: 4,829 bytes in 56 blocks
==11398== still reachable: 48,348 bytes in 277 blocks
==11398== suppressed: 0 bytes in 0 blocks
==11398== Rerun with --leak-check=full to see details of leaked memory
No. To delete the memory you allocate correctly you need to call delete:
However manually managing memory like this is difficult and error prone. You should prefer alternatives. The first is that you should not use dynamic allocation at all. You should simply use automatic variables:
If you really do need dynamic allocation you should use smart pointers.
Smart pointers implement RAII for dynamic allocation so you don’t have to do it manually.
In some rare circumstances you may actually need to do dynamic allocation manually, but that’s an advance topic.
std::vector<T>::clear()is not required to deallocate the vector’s memory. You can use the C++11 member functionshrink_to_fit(), or you can use the swap trick:Also you really shouldn’t be using a pointer to a vector. A vector uses RAII to manage dynamic memory for you. When you use a pointer to a vector you no longer have the benefit of not manually managing the resource yourself.