7

我观察到我的 MSVC10 副本带有似乎允许基于状态的分配器的容器,并编写了一个简单的池分配器,为特定类型分配池。但是,我发现如果_ITERATOR_DEBUG_LEVEL != 0MSVC 向量从传递的分配器创建一个代理分配器(用于迭代器跟踪?),使用代理,然后让代理超出范围,期望分配的内存保留。这会导致问题,因为我的分配器试图在销毁时释放它的池。这是 C++0x 标准允许的吗?

代码大致是这样的:

class _Container_proxy{};

template<class T, class _Alloc>
class vector {
     _Alloc _Alval;
public: 
    vector() {
        // construct _Alloc<_Container_proxy> _Alproxy
         typename _Alloc::template rebind<_Container_proxy>::other 
                 _Alproxy(_Alval);
        //allocate
        this->_Myproxy = _Alproxy.allocate(1);
        /*other stuff, but no deallocation*/
    } //_Alproxy goes out of scope

    ~_Vector_val() {    // destroy proxy
        // construct _Alloc<_Container_proxy> _Alproxy
        typename _Alloc::template rebind<_Container_proxy>::other
                 _Alproxy(_Alval);
        /*stuff, but no allocation*/
        _Alproxy.deallocate(this->_Myproxy, 1);
    } //_Alproxy goes out of scope again
4

1 回答 1

2

根据第 17.6.3.5 节中的分配器要求巨表,分配器必须是可复制的。容器可以自由复制它们。因此,您需要将池存储在 astd::shared_ptr或类似的东西中,以防止在分配器之一存在时删除。

于 2011-08-30T23:08:12.570 回答