Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Does destructor get called on std::map elements when std::map::clear is used?

I tried to debug for std::map<string,string> but could not see std::string destructor getting invoked. Can any one please help my understanding?

Documentation states it gets called, but I could not notice it.

Documentation is right, it does get called.

The destruction will be done by the method std::allocator<T>::deallocate() . Trace through that in your debugger.

http://www.cplusplus.com/reference/std/memory/allocator/

Some how I missed it, but I could see my break point being hit at std::string destructor, and yes indeed the call stack came from std::allocator. Thanks for your time John. Vikas Putcha Jan 23, 2013 at 14:47 public: A() { std::cout << "Constructor " << this << std::endl; } A(const A& other) { std::cout << "Copy Constructor " << this << std::endl; } ~A() { std::cout << "Destructor " << this <<std::endl; } int main() std::map<std::string, A> mp; mp.insert(std::pair<std::string, A>("hello", a)); mp.clear(); std::cout << "Ending" << std::endl;

This will report an output similar to this:

Constructor 0xbf8ba47a
Copy Constructor 0xbf8ba484
Copy Constructor 0xbf8ba48c
Copy Constructor 0x950f034
Destructor 0xbf8ba48c
Destructor 0xbf8ba484
Destructor 0x950f034
Ending
Destructor 0xbf8ba47a

So, you can see that the destructors get called by the calling the clear function.

Yes I agree with your code, but I was aiming at std::string objects rather than user defined objects. Thanks for your time Chris. – Vikas Putcha Jan 23, 2013 at 14:34 True, but i was looking for std::string though, my question got answered by the below comment, the reference link served the purpose to re-check my understanding, as it improved assurance level in my thinking. – Vikas Putcha Jan 23, 2013 at 14:51

Here is a bit more of a complete test, building on Chris Mansley's code as I wanted to see the effect on values, ptrs and refs - and I wanted to see the difference between clear and erase. No difference. In summary the destructor is only called for value types, which you would expect. I just like to check my understanding 8)

#include <iostream>
#include <map>
class A
public:
    std::string some_data;
    A(std::string some_data) : some_data(some_data) {
        std::cout << "  A(" << some_data << ") @" << this << std::endl;
    A(const A& other) {
        some_data = other.some_data;
        std::cout << "  Copy A(" << other.some_data << ") @" << this << std::endl;
    ~A() {
        std::cout << "  Destruct ~A(" << some_data << ") @" << this << std::endl;
void clear_test_value (void)
    std::cout << "clear_test_value() {" << std::endl;
    std::map<std::string, A> mp;
    A a("A1 data");
    mp.insert(std::pair<std::string, A>("key1", a));
    mp.clear();
    std::cout << "}" << std::endl;
    std::cout << std::endl;
void erase_test_value (void)
    std::cout << "erase_test_value() {" << std::endl;
    std::map<std::string, A> mp;
    A a("A1 data");
    mp.insert(std::pair<std::string, A>("key2", a));
    auto f = mp.find("key2");
    if (f == mp.end()) {
        std::cout << "failed to find element {" << std::endl;
        return;
    mp.erase(f);
    std::cout << "}" << std::endl;
    std::cout << std::endl;
void clear_test_ptr (void)
    std::cout << "clear_test_ptr() {" << std::endl;
    std::map<std::string, A*> mp;
    A a("A1 data");
    mp.insert(std::pair<std::string, A*>("key1", &a));
    mp.clear();
    std::cout << "}" << std::endl;
    std::cout << std::endl;
void erase_test_ptr (void)
    std::cout << "erase_test() {" << std::endl;
    std::map<std::string, A*> mp;
    A a("A1 data");
    mp.insert(std::pair<std::string, A*>("key2", &a));
    auto f = mp.find("key2");
    if (f == mp.end()) {
        std::cout << "failed to find element {" << std::endl;
        return;
    mp.erase(f);
    std::cout << "}" << std::endl;
    std::cout << std::endl;
void clear_test_ref (void)
    std::cout << "clear_test_ref() {" << std::endl;
    std::map<std::string, A&> mp;
    A a("A1 data");
    mp.insert(std::pair<std::string, A&>("key1", a));
    mp.clear();
    std::cout << "}" << std::endl;
    std::cout << std::endl;
void erase_test_ref (void)
    std::cout << "erase_test_ref() {" << std::endl;
    std::map<std::string, A&> mp;
    A a("A1 data");
    mp.insert(std::pair<std::string, A&>("key2", a));
    auto f = mp.find("key2");
    if (f == mp.end()) {
        std::cout << "failed to find element {" << std::endl;
        return;
    mp.erase(f);
    std::cout << "}" << std::endl;
    std::cout << std::endl;
int main ()
    clear_test_value();
    erase_test_value();
    clear_test_ptr();
    erase_test_ptr();
    clear_test_ref();
    erase_test_ref();
    return (0);

Output:

clear_test_value() {
  A(A1 data) @0x7ffee07389a0
  Copy A(A1 data) @0x7ffee0738960
  Copy A(A1 data) @0x7fe98fc029c8
  Destruct ~A(A1 data) @0x7ffee0738960
  Destruct ~A(A1 data) @0x7fe98fc029c8
  Destruct ~A(A1 data) @0x7ffee07389a0
erase_test_value() {
  A(A1 data) @0x7ffee07387f0
  Copy A(A1 data) @0x7ffee07387b0
  Copy A(A1 data) @0x7fe98fc029c8
  Destruct ~A(A1 data) @0x7ffee07387b0
  Destruct ~A(A1 data) @0x7fe98fc029c8
  Destruct ~A(A1 data) @0x7ffee07387f0
clear_test_ptr() {
  A(A1 data) @0x7ffee07389b0
  Destruct ~A(A1 data) @0x7ffee07389b0
erase_test() {
  A(A1 data) @0x7ffee0738800
  Destruct ~A(A1 data) @0x7ffee0738800
clear_test_ref() {
  A(A1 data) @0x7ffee07389b0
  Destruct ~A(A1 data) @0x7ffee07389b0
erase_test_ref() {
  A(A1 data) @0x7ffee0738800
  Destruct ~A(A1 data) @0x7ffee0738800
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.