向Std::vector添加结构体时使用Std::bad_alloc
std::bad_alloc when adding a struct to std::vector
这可能是愚蠢的事情,但我不明白。我在以下代码片段中得到std::bad_alloc
异常(这是交换机中的case语句):
case 0:
{
MyPrimitiveNode* node = new MyPrimitiveNode( 1, false );
TheStack.push_back( MyStackItem( node, TYPE_REF ) ); // bad_alloc here
break;
}
其中TheStack
为MyStack
型,为typedef std::vector<MyStackItem> MyStack;
MyStackItem
是一个简单的结构,看起来像这样:
struct MyStackItem {
MyNode* value;
uint8_t type;
MyStackItem() {
value = NULL;
type = TYPE_UNDEF;
}
MyStackItem( MyNode* val, uint8_t t ) {
value = val;
type = t;
}
};
对于MyNode
和MyPrimitiveNode
,它们来自另一个项目(一个静态库),定义如下:
class MyNode
{
public:
MyNode() {}
virtual ~MyNode() {}
};
class MyPrimitiveNode : public MyNode
{
public:
bool bDeclaration;
uint32_t u32ObjectIdx;
MyPrimitiveNode() {
bDeclaration = false;
u32ObjectIdx = 0;
}
MyPrimitiveNode( uint32_t id, bool declaration ) {
bDeclaration = declaration ;
u32ObjectIdx = id;
}
~MyPrimitiveNode() {}
};
希望这是所有需要的相关信息。我知道MyStackItem只做一个浅拷贝,这就是我想要的。不要担心泄漏,那是别处处理的。
有人能告诉我是怎么回事,我怎么能解决它?谢谢。
编辑:发布堆栈跟踪可能会有所帮助:
> myProgram.exe!std::_Construct<MyStackItem,MyStackItem>(MyStackItem* _Ptr=0x003d3de8, const MyStackItem& _Val={...}) Line 52 + 0x33 bytes C++
myProgram.exe!std::allocator<MyStackItem>::construct(MyStackItem* _Ptr=0x003d3de8, const MyStackItem& _Val={...}) Line 155 + 0xd bytes C++
myProgram.exe!std::_Uninit_fill_n<MyStackItem*,unsigned int,MyStackItem,std::allocator<MyStackItem> >(MyStackItem* _First=0x003d3de8, unsigned int _Count=0x00000001, const MyStackItem& _Val={...}, std::allocator<MyStackItem> & _Al={...}, std::_Nonscalar_ptr_iterator_tag __formal={...}, std::_Nonscalar_ptr_iterator_tag __formal={...}) Line 400 + 0x10 bytes C++
myProgram.exe!stdext::unchecked_uninitialized_fill_n<MyStackItem*,unsigned int,MyStackItem,std::allocator<MyStackItem> >(MyStackItem* _First=0x003d3de8, unsigned int _Count=0x00000001, const MyStackItem& _Val={...}, std::allocator<MyStackItem> & _Al={...}) Line 922 + 0x43 bytes C++
myProgram.exe!std::vector<MyStackItem,std::allocator<MyStackItem> >::_Ufill(MyStackItem* _Ptr=0x003d3de8, unsigned int _Count=0x00000001, const MyStackItem& _Val={...}) Line 1252 + 0x18 bytes C++
myProgram.exe!std::vector<MyStackItem,std::allocator<MyStackItem> >::_Insert_n(std::_Vector_const_iterator<MyStackItem,std::allocator<MyStackItem> > _Where={value={...} type=??? }, unsigned int _Count=0x00000001, const MyStackItem& _Val={...}) Line 1184 + 0x14 bytes C++
myProgram.exe!std::vector<MyStackItem,std::allocator<MyStackItem> >::insert(std::_Vector_const_iterator<MyStackItem,std::allocator<MyStackItem> > _Where={value={...} type=??? }, const MyStackItem& _Val={...}) Line 878 C++
myProgram.exe!std::vector<MyStackItem,std::allocator<MyStackItem> >::push_back(const MyStackItem& _Val={...}) Line 823 + 0x58 bytes C++
myProgram.exe!MethodWhereExceptionOccurs
堆栈跟踪没有显示任何让我相信push_back
的某些部分正在请求大量内存的东西。
因此,这几乎留下了一个选项,即您的程序在其他地方破坏了堆,而这个分配是受害者。如果没有更多的代码和细节,我能建议的就是像valgrind这样的内存检查器。
MyStackItem
有你没有给我们看的毁灭吗?
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- "std::vector"在调整大小时引发"bad allocation"异常
- 成员函数 bad() 的 std::ftsream 用于检查
- 正在使用std::string而不是char*bad
- 在抛出 'std::bad _alloc' 的实例后调用终止 what(): std::bad_alloc 在 c++ 中
- What is Scala for: getline(), std::cin.eof(), std::cin.bad()