3

这可能是愚蠢的,但我无法弄清楚。我std::bad_alloc在以下代码片段中遇到异常(这是 switch 中的 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;
    }
};

至于MyNodenad 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
4

1 回答 1

7

该堆栈跟踪没有显示任何让我相信其中的某些部分push_back正在请求大量内存的内容。

因此,这几乎留下了您的程序在 ELSE 某处损坏堆并且此分配是受害者的选项。如果没有更多代码和细节,我只能建议像 valgrind 这样的内存检查器。

有没有MyStackItem你没有向我们展示的破坏?

于 2011-04-21T13:18:57.883 回答