开发者

std::bad_alloc when adding a struct to std::vector

this is probably something stupid, but i can't figure it out. I'm getting a std::bad_alloc exception in the following code snippet (which is a case statement in a switch):

case 0:
{
MyPrimitiveNode* node = new MyPrimitiveNode( 1, false );
TheStack.push_back( MyStackItem( node, TYPE_REF ) ); // bad_alloc here
break;
}

Where TheStack is of type MyStack, which is a typedef std::vector<MyStackItem> MyStack;

MyStackItem is a simple structure and looks like this:

struct MyStackItem {
    MyNode* value;
    uint8_t type;

    MyStackItem() {
        value = NULL;
        type = TYPE_UNDEF;
    }

    MyStackItem( MyNode* val, uint8_t t ) {
        value = val;
        type = t;
    }
};

As for MyNode nad MyPrimitiveNode, they come from another project (a static lib) and are defined as follows:

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() {}
    };

Hope this is all relevant info that is needed. I know that MyStackItem does only a shallow copy, this is how i want it. Dont worry about leaks, thats handled elsewhere.

Can someone tell my what is going on and how can i fix it? Thanks.

EDIT: posting the stack trace might help:

>   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


That stack trace doesn't show anything that would lead me to believe that some part of the push_back is requesting a ton of memory.

Therefore, that pretty much leaves the option that your program has corrupted the heap somewhere ELSE and this allocation is the victim. Without more code and details all I can suggest is a memory checker like valgrind.

Does MyStackItem have a destruction you aren't showing us?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜