tags:

views:

158

answers:

2

I am currently working on a project that deals with a vector of objects of a People class. The program compiles and runs just fine, but when I use the debugger it dies when trying to do anything with the PersonWrangler object. I currently have 3 different classes, one for the person, a personwrangler which handles all of the people collectively, and a game class that handles the game input and output.

Edit: My basic question is to understand why it is dying when it calls outputPeople. Also I would like to understand why my program works exactly as it should unless I use the debugger. The outputPeople function works the way I intended that way.

Edit 2: The callstack has 3 bad calls which are:

  1. std::vector >::begin(this=0xbaadf00d)
  2. std::vector >::size(this=0xbaadf00d)
  3. PersonWrangler::outputPeople(this=0xbaadf00d)

Relevant code:

class Game
{
public:
    Game();
    void gameLoop();
    void menu();
    void setStatus(bool inputStatus);
    bool getStatus();
    PersonWrangler* hal;
private:
    bool status;
};

which calls outputPeople where it promptly dies from a baadf00d error.

void Game::menu()
{
    hal->outputPeople();
}

where hal is an object of PersonWrangler type

class PersonWrangler
{
public:
    PersonWrangler(int inputStartingNum);
    void outputPeople();  
    vector<Person*> peopleVector;
    vector<Person*>::iterator personIterator;  
    int totalPeople;
};

and the outputPeople function is defined as

void PersonWrangler::outputPeople()
{
    int totalConnections = 0;
    cout << " Total People:" << peopleVector.size() << endl;
    for (unsigned int i = 0;i < peopleVector.size();i++)
    {
        sort(peopleVector[i]->connectionsVector.begin(),peopleVector[i]->connectionsVector.end());
        peopleVector[i]->connectionsVector.erase( unique (peopleVector[i]->connectionsVector.begin(),peopleVector[i]->connectionsVector.end()),peopleVector[i]->connectionsVector.end());
        peopleVector[i]->outputPerson();
        totalConnections+=peopleVector[i]->connectionsVector.size();
    }
    cout << "Total connections:" << totalConnections/2 << endl;
}

Where hal is initialized

Game::Game()
{
    PersonWrangler* hal = new PersonWrangler(inputStartingNum);
}
+1  A: 

Did you initialize hal to point to an actual PersonWrangler object?

Creating a pointer does not point it at an actual object unless you do it explicitly. You probably want to either pass a PersonWrangler to your Game at construction time, or have the Game constructor create a PersonWrangler using new. If you choose the latter, make sure to delete your PersonWrangler somewhere, probably in the Game deconstructor.

Greg Howell
+6  A: 

0xBAADFOOD is a magic number to alert you to the fact that you're dealing with uninitialized memory. From the stack trace, we see that this in PersonWrangler::outputPeople is invalid. Thus hal doesn't point to a valid PersonWrangler (that is, assuming frame 4 is a call to Game::menu). Step through the code, starting at Game::Game(), examining Game::hal as you go, to see what might be going wrong.

Debuggers fill uninitialized memory with magic numbers to make it easier to spot errors. In a production build, memory isn't filled with anything in particular; the content of uninitialized memory is undefined, and might hold valid values. This is why a production build might not fail when a debug build will.

outis