使用boost池分配器的allocate_shared

时间:2018-03-28 04:25:24

标签: shared-ptr c++17 boost-pool

我正在尝试将std :: allocate_shared与boost池分配器一起使用,程序运行正常,但在一段时间后它会在共享指针析构函数中与segfault崩溃。我想知道我的实现是否有错误。我在这里粘贴分配器,我在ubuntu上使用gcc-7.2编译器。请指教。

#pragma once
#include <memory>
#include <utility>
#include <boost/pool/pool_alloc.hpp>
#include <assert.h>
#include <cstddef>
#include <type_traits>
#include <scoped_allocator>

template <typename T = void>
struct _shpAllocator
{
    template <typename U> friend struct _shpAllocator;
    using value_type = T;
    using pointer = T *;
    using propagate_on_container_copy_assignment = std::true_type;
    using propagate_on_container_move_assignment = std::true_type;
    using propagate_on_container_swap = std::true_type;
    using is_always_equal = std::true_type;

    explicit _shpAllocator() {}
    template <typename U> _shpAllocator(_shpAllocator<U> const & rhs) {}

    pointer allocate(std::size_t n)
    {

        std::cerr<<"_shpAllocator() typename : "<<typeid(T).name()<<std::endl;
        auto p = __fpAlloc.allocate ( n );
        return p;
    }

    void deallocate(pointer p, std::size_t n)
    {

        __fpAlloc.deallocate ( p, n );
        return;
    }
    template <typename U> bool operator==(_shpAllocator<U> const & rhs) const { return true; }
    template <typename U> bool operator!=(_shpAllocator<U> const & rhs) const { return false; }

    private:
    static boost::pool_allocator<T, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex> __fpAlloc;
    static sLock __lock;
};

template<typename T> boost::pool_allocator<T, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex> _shpAllocator<T>::__fpAlloc;
template<typename T> sLock _shpAllocator<T>::__lock;

template<typename T> using shpAllocator = std::scoped_allocator_adaptor<_shpAllocator<T>>;
template<typename T> using sharedObjectAllocator = shpAllocator<T>;

崩溃追踪。

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000767cc5 in std::_Sp_counted_ptr_inplace<

0 个答案:

没有答案