当我将 c++0x 初始化器列表与向量一起使用时,我遇到了段错误。我不知道为什么会这样。我的调试器说崩溃发生在标准库中的这个函数上:
template<typename _T1, typename _T2>
inline void
#ifdef __GXX_EXPERIMENTAL_CXX0X__
// Allow perfect forwarding
_Construct(_T1* __p, _T2&& __value)
#else
_Construct(_T1* __p, const _T2& __value)
#endif
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 402. wrong new expression in [some_]allocator::construct
::new(static_cast<void*>(__p)) _T1(_GLIBCXX_FORWARD(_T2, __value));
}
我试图确定此功能的用途,但在网上找不到任何解释/文档。
在我的代码中使用初始化列表的代码如下所示:
bool Cube::ProcessData(MeshData* data)
{
data->Clear();
data->v =
{
Vec3(.5,-.5,-.5), Vec3(.5,-.5,.5), Vec3(-.5,-.5,.5), Vec3(-.5,-.5,-.5),
Vec3(.5, .5,-.5), Vec3(.5, .5,.5), Vec3(-.5, .5,.5), Vec3(-.5, .5,-.5)
};
...
}
传递给此函数的数据结构在此处创建:
template <class ProcessorT, class DataT, typename... Args>
const DataT* DataManager::RequestData(Args... args)
{
MutexLock lock(*mutex);
Request req;
data_cache.PushBack();
req.data = &data_cache.GetBack();
req.processor = new ProcessorT(args...);
request_list.push_back(req);
return static_cast<DataT*>(req.data);
}
data_cache 结构是我自己用来避免复制的列表类。ProcessData 函数在与创建数据结构的线程不同的线程上调用。
这是调用堆栈的调试器输出:
#0 004FAAD6 _Construct<UtilityLib::TVec3<float>, UtilityLib::TVec3<float> const&>(this=0x104aba0, __first=0x593fb98, __last=0x593fbf8) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_construct.h:80)
#1 00000000 uninitialized_copy<UtilityLib::TVec3<float> const*, UtilityLib::TVec3<float>*>(this=0x104aba0, __first=0x593fb98, __last=0x593fbf8) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_uninitialized.h:74)
#2 00000000 uninitialized_copy<UtilityLib::TVec3<float> const*, UtilityLib::TVec3<float>*>(this=0x104aba0, __first=0x593fb98, __last=0x593fbf8) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_uninitialized.h:116)
#3 00000000 __uninitialized_copy_a<UtilityLib::TVec3<float> const*, UtilityLib::TVec3<float>*, UtilityLib::TVec3<float> >(this=0x104aba0, __first=0x593fb98, __last=0x593fbf8) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_uninitialized.h:318)
#4 00000000 std::vector<UtilityLib::TVec3<float>, std::allocator<UtilityLib::TVec3<float> > >::_M_assign_aux<UtilityLib::TVec3<float> const*>(this=0x104aba0, __first=0x593fb98, __last=0x593fbf8) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/vector.tcc:260)
#5 004127B3 _M_assign_dispatch<UtilityLib::TVec3<float> const*>(this=0x6e8af18, data=0x104ab98) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_vector.h:1065)
#6 00000000 assign<UtilityLib::TVec3<float> const*>(this=0x6e8af18, data=0x104ab98) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_vector.h:396)
#7 00000000 operator=(this=0x6e8af18, data=0x104ab98) (c:/mingw/bin/../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_vector.h:359)
#8 00000000 GameEngine::Render3D::Cube::ProcessData(this=0x6e8af18, data=0x104ab98) (C:\CodeBlocksProjects\GameEngine\src\Primitives.cpp:56)
我怀疑我的列表类可能是罪魁祸首,但即使是我也不知道为什么。希望 StackOverflow 上的人可以帮助我理解这个问题。我感谢任何意见或建议。