我在使用 Howard Hinnant 时遇到崩溃基于堆栈的分配器 http://howardhinnant.github.io/stack_alloc.h,都在
MacOS 和 64 位 Linux 上的 Clang 3.4。这是一个最小的例子
在容器的析构函数中触发崩溃:
#include <vector>
#include "stack_alloc.h"
template <template <typename...> class C, typename T, size_t N>
using stack_container = C<T, stack_alloc<T, N>>;
using stack_vector = stack_container<std::vector, size_t, 4>;
int main()
{
auto s = stack_vector{1, 2, 3};
auto m = std::move(s);
}
编译如下:
clang++ -std=c++11 -stdlib=libc++ -g -Wall crash.cc && ./a.out
您知道为什么会发生这次事故吗?我也尝试过
重新实现stack_alloc
就竞技场实施而言短分配 http://howardhinnant.github.io/short_alloc.h,但在移动基于堆栈的容器时我仍然遇到崩溃。
这是一个 Linux 回溯:
#0 _int_free (av=0x394f5b8760 <main_arena>, p=0x7fffffffe0f0, have_lock=0) at malloc.c:3901
#1 0x00000000004013eb in stack_alloc<unsigned long, 4ul>::deallocate (this=0x7fffffffe080, p=0x7fffffffe100, n=3)
at ./stack_alloc.h:71
#2 0x0000000000401343 in capacity (this=0x7fffffffe060, this=0x7fffffffe060, __a=..., __p=0x7fffffffe100, __n=3)
at .../include/c++/v1/memory:1443
#3 std::__1::__vector_base<unsigned long, stack_alloc<unsigned long, 4> >::~__vector_base (this=0x7fffffffe060)
at .../include/c++/v1/vector:476
#4 0x0000000000400fa5 in std::__1::vector<unsigned long, stack_alloc<unsigned long, 4> >::~vector (this=0x7fffffffe060)
at .../include/c++/v1/vector:481
#5 0x0000000000400f6e in main () at crash.cc:13
我有兴趣(i)如果人们可以重现该错误,并且(ii)如何修复它。