开发者

C++ Vector of Pointers, too many passes?

I've been trying to figure out how to solve this seg-fault problem for hours now.

I have a card game in C++, four players. A player (base class) can either be a computer or a human (derived classes). To store them, I have another class called playerContainer, which has its own functions and uses a private member vector of player pointers.

std::vector<Player*> players_;

When I add a new player to the vector, I do something like this:

Human *newPlayer = new Human();
container.add(newPlayer);

Where add is:

void playerContainer::add(Player* newPlayer) {
   players_.push_back(newPlayer);
}

A开发者_Go百科nd then I have one class called gameLogic which has a private member playerContainer and performs the player initialization (among several other things).

However, I'm getting seg faults after trying to make incorporate it into an MVC implementation.

On the GUI, you click a button to start the game. The button calls the window's startGame function, which gathers parameters and then calls its private member Model class's startGame, which then calls its private member gameLogic's initializePlayer function, which then calls its private member playerContainer's add function. At that point the program will break.

On a bit of testing, I found that it would not seg fault if I saved a step. For example, if I gave the window object a gameLogic object, and then called its initializePlayer function and so forth, the program would proceed normally without error.

I'm certain there's a better solution than to combine the Model and gameLogic classes as they are already quite bloated. May anyone provide some insight?

Also, if this is any bit confusing, please ask me to clarify. I did my best to explain clearly, but with so many different classes involved, it gets difficult to do so.

EDIT: Can't try replacing Player* with int, the code's way too huge for me to be able to do this in any timely fashion.

I HAVE tried valgrind, but the log was huge and generally unhelpful. Or at the very least, I couldn't understand much of it. For those who want to take a look, here's a scribd link: http://www.scribd.com/doc/60210916/Memory-Leak

The seg fault is being raised on the playerContainer's add function, on the push_back line.

I'll try my hand at the debugger...


Are you sure your Model, somewhere, but before initializePlayer is called, actually allocates the playerContainer?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜