When I run the next code in VS2017:
#include <boost/pool/pool_alloc.hpp>
#include <map>
#include <iostream>
int main()
{
using Map = std::map<int, int, std::less<int>, boost::pool_allocator<std::pair<const int, int>>>;
using Pool = boost::singleton_pool<boost::pool_allocator_tag, sizeof(Map)>;
Map temp;
for (int i = 1; i < 5; i++) temp[i] = i;
std::cout << "First addresses:\n";
for (auto& kv : temp) std::cout << &kv.second << "\n";
temp.clear();
Pool::purge_memory();
Map temp2;
for (int i = 1; i < 5; i++) temp2[i] = i;
std::cout << "Second addresses:\n";
for (auto& kv : temp2) std::cout << &kv.second << "\n";
temp2.clear();
Pool::purge_memory();
return 0;
}
I get the output:
First addresses:
02A108F4
02A1090C
02A10924
02A1093C
Second addresses:
02A1090C
02A10924
02A1093C
02A10954
This behavior seems incorrect: what happened to address 02A108F4
? It seems it was not returned to the pool during the purge.
This doesn't happen when I use a std::vector
instead of a std::map
.
gcc also seems to return the memory correctly: Live example.
Is this a bug in VS2017?