Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std:map Destructor call Key Destructors as well as Value Destructors?

For example, does the following leak?

Foo ( )
{
   std:map<std:string, int> myMap;
   myMap[std::string("Bar")] = 2983;
}

I believe it does not leak but can't find specific documentation on this point.

like image 752
ThomasMcLeod Avatar asked Jan 13 '23 05:01

ThomasMcLeod


2 Answers

Yes, map destructor map::~map() will call destructor for every key and value it manages and free memory.

§ 23.2.1 Table 96 — Container requirements (continued)

(&a)->X() void 
the destructor is applied to every element of a; all the memory is deallocated.
like image 58
billz Avatar answered Jan 14 '23 19:01

billz


Yes, it certainly does. This is pretty standard stuff in C++, and basically everything in the standard library and STL works this way--destructors are always called unless you're storing raw pointers.

like image 30
John Zwinck Avatar answered Jan 14 '23 20:01

John Zwinck