我创建了一个自定义分配器,该分配器在构造时分配内存,并在破坏时释放它。 (以允许快速分配/解除分配)。 当我将其与STL容器一起使用时,一切正常!预期当我使用assign方法时... 我不明白为什么...
我试图打印分配/释放的每个指针,但是看起来都不错。
#include <cstddef>
#include <type_traits>
#include <stack>
#include <numeric>
#include <list>
template <class T>
class CustomAllocator
{
public:
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
using is_always_equal = std::false_type;
CustomAllocator();
CustomAllocator(size_type size);
~CustomAllocator();
CustomAllocator(const CustomAllocator&);
CustomAllocator& operator=(const CustomAllocator&) = delete;
CustomAllocator(CustomAllocator&& src)
: m_data(std::move(src.m_data)) , m_free(std::move(src.m_free))
{
src.m_data = nullptr;
}
CustomAllocator& operator=(CustomAllocator&&) = delete;
template <class T2>
CustomAllocator(const CustomAllocator<T2>&);
template <class T2>
bool operator==(const CustomAllocator<T2>&) const noexcept;
template <class T2>
bool operator!=(const CustomAllocator<T2>&) const noexcept;
value_type* allocate(size_type);
void deallocate(value_type* ptr, size_type);
private:
template <class>
friend class CustomAllocator;
void* m_data = nullptr;
std::stack<void*> m_free;
};
template <class T>
CustomAllocator<T>::CustomAllocator() : CustomAllocator(1024)
{
}
template <class T>
CustomAllocator<T>::CustomAllocator(size_type size)
{
m_data = ::operator new(sizeof(T) * size);
for (auto ptr = static_cast<T*>(m_data) + (size - 1); ptr >=
static_cast<T*>(m_data); ptr--)
m_free.push(ptr);
}
template <class T>
CustomAllocator<T>::CustomAllocator(const CustomAllocator&)
: CustomAllocator(1024)
{
}
template <class T>
template <class T2>
CustomAllocator<T>::CustomAllocator(const CustomAllocator<T2>&)
: CustomAllocator(1024)
{
}
template <class T>
CustomAllocator<T>::~CustomAllocator()
{
if (m_data)
::operator delete(m_data);
}
template <class T>
template <class T2>
inline bool CustomAllocator<T>::
operator==(const CustomAllocator<T2>&) const noexcept
{
return typeid(T) == typeid(T2);
}
template <class T>
template <class T2>
inline bool CustomAllocator<T>::
operator!=(const CustomAllocator<T2>&) const noexcept
{
return typeid(T) != typeid(T2);
}
template <class T>
typename CustomAllocator<T>::value_type*
CustomAllocator<T>::allocate(size_type size)
{
if (m_free.empty() || size != 1)
throw std::bad_alloc();
auto ptr = m_free.top();
m_free.pop();
return reinterpret_cast<value_type*>(ptr);
}
template <class T>
void CustomAllocator<T>::deallocate(value_type* ptr, size_type)
{
m_free.push(ptr);
}
int main()
{
std::list<size_t, CustomAllocator<size_t>> containerA;
std::list<size_t, CustomAllocator<size_t>> containerB;
for (size_t i = 0; i < 10; ++i)
{
for (size_t j = 0; j < 100; ++j)
containerA.emplace_front();
// dont works with this
containerB.assign(10, i);
containerA.clear();
containerB.clear();
}
return 0;
}
实际上是程序崩溃。 如果我注释“ containerB.assign(10,i);”,则该程序将运行。 如果我替换为'containerB.assign(10,i);'通过“ containerB.emplace_front();”,该程序可以工作。 如果我替换为'containerB.assign(10,i);'通过'containerB.insert(containerB.begin(),10,i);',程序崩溃。 我不明白为什么...
clang和gcc错误: free():损坏的未排序块 中止(核心已转储)
gdb错误: free():损坏的未排序块
程序收到信号SIGABRT,异常终止。 __GI_raise(sig = sig @ entry = 6),位于../sysdeps/unix/sysv/linux/raise.c:51 51 ../sysdeps/unix/sysv/linux/raise.c:没有这样的文件或目录。
更新:
有了更好的运算符==,我现在有了SIGSEGV: 程序收到信号SIGSEGV,分段故障。 在/usr/include/c++/8/bits/list.tcc:74处的std :: __ cxx11 :: _ List_base> :: _ M_clear(this = 0x7fffffffdcd0)中为0x000055555555537a 74 __cur = __tmp-> _ M_next;
valgrind输出:
==17407== Memcheck, a memory error detector
==17407== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==17407== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==17407== Command: ./a.out
==17407==
==17407== Invalid read of size 8
==17407== at 0x10937A: std::__cxx11::_List_base<unsigned long, CustomAllocator<unsigned long> >::_M_clear() (list.tcc:74)
==17407== by 0x109287: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::clear() (stl_list.h:1507)
==17407== by 0x108F0C: main (bug.cpp:154)
==17407== Address 0x5b93b00 is 0 bytes inside a block of size 24,576 free'd
==17407== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17407== by 0x1091CE: CustomAllocator<std::_List_node<unsigned long> >::~CustomAllocator() (bug.cpp:100)
==17407== by 0x109107: std::__cxx11::_List_base<unsigned long, CustomAllocator<unsigned long> >::_List_impl::~_List_impl() (stl_list.h:382)
==17407== by 0x109205: std::__cxx11::_List_base<unsigned long, CustomAllocator<unsigned long> >::~_List_base() (stl_list.h:506)
==17407== by 0x10915B: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::~list() (stl_list.h:834)
==17407== by 0x109B66: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::insert(std::_List_const_iterator<unsigned long>, unsigned long, unsigned long const&) (list.tcc:122)
==17407== by 0x109586: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::_M_fill_assign(unsigned long, unsigned long const&) (list.tcc:300)
==17407== by 0x10926C: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::assign(unsigned long, unsigned long const&) (stl_list.h:897)
==17407== by 0x108EEE: main (bug.cpp:151)
==17407== Block was alloc'd at
==17407== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17407== by 0x109665: CustomAllocator<std::_List_node<unsigned long> >::CustomAllocator(unsigned long) (bug.cpp:76)
==17407== by 0x10A3B6: CustomAllocator<std::_List_node<unsigned long> >::CustomAllocator<unsigned long>(CustomAllocator<unsigned long> const&) (bug.cpp:92)
==17407== by 0x109FFE: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::list(unsigned long, unsigned long const&, CustomAllocator<unsigned long> const&) (stl_list.h:717)
==17407== by 0x109B0B: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::insert(std::_List_const_iterator<unsigned long>, unsigned long, unsigned long const&) (list.tcc:122)
==17407== by 0x109586: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::_M_fill_assign(unsigned long, unsigned long const&) (list.tcc:300)
==17407== by 0x10926C: std::__cxx11::list<unsigned long, CustomAllocator<unsigned long> >::assign(unsigned long, unsigned long const&) (stl_list.h:897)
==17407== by 0x108EEE: main (bug.cpp:151)
==17407==
==17407==
==17407== HEAP SUMMARY:
==17407== in use at exit: 0 bytes in 0 blocks
==17407== total heap usage: 504 allocs, 504 frees, 668,800 bytes allocated
==17407==
==17407== All heap blocks were freed -- no leaks are possible
==17407==
==17407== For counts of detected and suppressed errors, rerun with: -v
==17407== ERROR SUMMARY: 100 errors from 1 contexts (suppressed: 0 from 0)
答案 0 :(得分:0)
m_data
是指向void
的指针。在下一行的m_data
的构造函数中,对CustomAllocator
执行了指针运算。
for (auto ptr = m_data + (sizeof(T) * (size - 1)); ptr >= m_data; ptr -= sizeof(T))
按照标准,void*
上的指针算术格式错误。这可能导致不确定的行为。
编辑
OP已更新,在void*
上不再执行任何指针算法。因此,我们必须查找导致崩溃的其他原因。
对于list
,容器emplace_front
不会影响迭代器和引用的有效性。但是assign
和clear
都使所有引用容器元素的引用,指针和迭代器无效。
因此,该程序可与emplace_front
一起使用,并与assign
一起崩溃。
答案 1 :(得分:0)
自C ++ 11起的分配器可以具有状态。
但是从同一分配器复制的分配器共享状态,因此原始分配器分配的某些内容应通过copy释放。
也应该用operator ==和operator!=比较状态,如果状态不同,分配器就不应相等。
这可以通过在分配器中具有指向潜在共享池的指针来实现,而不是直接在分配器中具有池。 (共享分配器的池有时称为“竞技场”。)
在C ++ 11之前,分配器根本没有状态。这样,即使使用默认构造的分配器(每次可能是不同的实例),也可以分配/取消分配对象。在这种情况下,您的等价运算符是正确的,但是您需要有一个全局池,而不是每个容器都有单独的池。
答案 2 :(得分:0)
在讨论之后,我实现了一个有状态的分配器,但是std :: list有问题。 我对此发布了一个问题:STL container don't support statefull allocator?