Does std::vector.clear() do delete (free memory) on each element?

C++Std

C++ Problem Overview


Consider this code:

#include <vector>

void Example()
{
    std::vector<TCHAR*> list;
    TCHAR* pLine = new TCHAR[20];
    list.push_back(pLine);
    list.clear();    // is delete called here?
    // is delete pLine; necessary?
}

Does list.clear() call delete on each element? I.e. do I have to free the memory before / after list.clear()?

C++ Solutions


Solution 1 - C++

std::vector does call the destructor of every element it contains when clear() is called. In your particular case, it destroys the pointer but the objects remain.

Smart pointers are the right way to go, but be careful. auto_ptr cannot be used in std containers. boost::scoped_ptr can't either. boost::shared_ptr can, but it won't work in your case because you don't have a pointer to an object, you are actually using an array. So the solution to your problem is to use boost::shared_array.

But I suggest you use std::basic_string<TCHAR> instead, where you won't have to deal with memory management, while still getting the benefits of working with a string.

Solution 2 - C++

No (you need to do the delete yourself at the end as you suggest in your example as the destruction of the bald pointer doesnt do anything). But you can use a boost [or other RAII-based idiom] smart pointer to make it Do The Right Thing (auto_ptr would not work correctly in a container as it has incompatible behaviour under copying etc.), but be sure you understand the pitfalls of such smart pointers before use. (As Benoit mentions, in this case, basic_string is what you're really looking for here.)

Having said that there's a need to understand the pitfalls of smart pointers, having them take care of the memory management implicitly so you dont have to do it explicitly is far less error-prone.

EDIT: Substantially revised to encompass the elements Benoit brought into his far more thorough answer, thanks to strong prodding from the Earwicker and James Matta - thanks for pushing me to do the due diligence on this!

Solution 3 - C++

Here's one way that you can tell that it doesn't - try it on a class that's not fully defined:

#include <vector>
class NotDefined;

void clearVector( std::vector<NotDefined*>& clearme )
{
    clearme.clear();    // is delete called here?
}

If this snippet compiles, then it can't be calling the destructor, because the destructor isn't defined.

Solution 4 - C++

You could just write a simple template function that does this for you:

template <class T>
void deleteInVector(vector<T*>* deleteme) {
    while(!deleteme->empty()) {
        delete deleteme->back();
        deleteme->pop_back();
    }

    delete deleteme;
}

Maybe something in here is bad practice but I don't think so. It looks okay to me though comments are always nice.

Solution 5 - C++

Nope. It doesn't do that since there is no guarantee you are not using the pointer anywhere else. If it wasn't a pointer variable, it would free them (by calling the destructor)

Solution 6 - C++

You might also be able to use the Boost Pointer Container Library. Not specifically recommended here (again because you're using arrays instead of single objects, though std::string would take care of that), but it's a useful and little-known library that solves the problem stated in the title.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIgnas LimanauskasView Question on Stackoverflow
Solution 1 - C++BenoîtView Answer on Stackoverflow
Solution 2 - C++Ruben BartelinkView Answer on Stackoverflow
Solution 3 - C++tfinnigaView Answer on Stackoverflow
Solution 4 - C++Robert MassaioliView Answer on Stackoverflow
Solution 5 - C++mmxView Answer on Stackoverflow
Solution 6 - C++Head GeekView Answer on Stackoverflow