【发布时间】:2018-11-04 20:28:27
【问题描述】:
当我在 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;
}
我得到了输出:
First addresses:
02A108F4
02A1090C
02A10924
02A1093C
Second addresses:
02A1090C
02A10924
02A1093C
02A10954
这种行为似乎不正确:地址02A108F4 发生了什么?在清除过程中它似乎没有返回到池中。
当我使用 std::vector 而不是 std::map 时,不会发生这种情况。
gcc 似乎也能正确返回内存:Live example。
这是 VS2017 中的错误吗?
【问题讨论】:
-
在您添加的实时示例中,该行为不会发生。
-
是的,我就是这么说的:在 GCC 中,活生生的例子,它有效。在 VC++ 中它没有。我将添加一个VC++的实例
-
现场示例 MSVC:rextester.com/BWANL51397
-
我写了所有这些作为答案:)
标签: c++ visual-studio boost pool stdmap