1

I'm running on a std::bad_alloc error. As far as I know and as I've seen researching on StackOverflow this can be caused by a lack of the necessary memory to perform an action or because there's some kind of corrupted datastructure (as explained here).

In my case, I've got a class A which contains an attribute std::vector<std::vector<unsigned int> > tiles. There's no error when this container is created in this way in A.h:

class A
{
    public:

        //function prototypes
        std::vector<std::vector<unsigned int> > GetTiles();

    protected:
    private:

        std::vector<std::vector<unsigned int> > tiles; //declaration

};

Additionnaly i've writen the prototype of a function GetTiles which is in charge of returning tiles. This is done in A.cpp with the following code:

std::vector<std::vector<unsigned int> > A::GetTiles()
{
    return tiles;
}

In a precise moment another class B intends to get the tiles container using GetTiles like this (B.cpp):

std::vector<std::vector<unsigned int> > aux;
aux=InstanceofA->GetTiles();                  

At that precise moment, after the call to GetTiles(), I get the following error:

terminate called after throwing an instance of std::bad_alloc

Therefore, it points out to an error when trying to allocate memory for the aux container. I've tried to make a bit of printf debugging by placing a printf() call in the GetTiles() function:

std::vector<std::vector<unsigned int> > A::GetTiles()
{
    printf("%i\n",tiles.size);
    return tiles;
}

Then, before crashing, the program shows a weird result on the console line: -1524170727. Before all this code there's nothing that might affect the tiles container and the other vectors declared in A.h behave normally and have 0 size after being created as everyone (as far as I understand) expects. I've also tried to place tiles.clear() call in the constructor of class A but it does nothing.

EDIT: I've also tried returning other containers in the GetTiles() function and it works. Additionally I've also tried calling the function without assigning it's returned value to any container, only:

InstanceofA->GetTiles()

And it also works, so the problem shouldn't be in the returned copy, but in the assignement of it to the aux container. I guess the error turns around the weird size of tiles.

I had never seen that before and I haven't found anything in Google. I'll appreciate any help you could give me. Thanks a lot. I apologize if there's any presentation mistake, it's only the second time I've posted something on SO.

Community
  • 1
  • 1
JuanGM
  • 25
  • 9
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/59963/discussion-on-question-by-user3869641-strange-stdbad-alloc-in-an-already-creat). – Andrew Barber Aug 25 '14 at 19:41

1 Answers1

1

Actually I've solved the problem following the steps pointed by many of the people who have commented my original post, so I really thank everybody. The problem was in the initialization of the pointer InstanceofA

JuanGM
  • 25
  • 9