【发布时间】:2014-08-07 23:38:58
【问题描述】:
我在使用 Howard Hinnant 的 stack-based allocator 时遇到崩溃,两者都在 带有 Clang 3.4 的 64 位 Linux 和 MacOS。这是一个最小的例子 在容器的析构函数中触发崩溃:
#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
short_alloc,但我在移动基于堆栈的容器时仍然会崩溃。
这是一个 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) 如何解决它。
【问题讨论】:
标签: c++ c++11 move-semantics allocator