BUGFIX: Solved server failure

Cause:

server/server_application.cpp: line 93

The last argument to std::pair was simply a call to WorldRoom's
contructor. This created a temporary object that fufilled this line, but
after the new std::pair object was added to worldRoomMap, this WorldRoom
object went out of scope.

server/server_application.cpp: line 96

When OpenRoom() was called using an object that was out of scope, the
entire server simply failed.

Solution:

Changed worldRoomMap to hold a pointer to a WorldRoom object, rather than the
object itself. The new and delete operators should be used to create and
delete WorldRoom objects respectfully.
This commit is contained in:
Kayne Ruse
2013-12-14 00:23:20 +11:00
parent 9df16fede0
commit baadf554cd
4 changed files with 6 additions and 24 deletions
+5 -10
View File
@@ -90,20 +90,14 @@ void ServerApplication::Init(int argc, char** argv) {
sqlite3_exec(database, script.c_str(), nullptr, nullptr, nullptr); sqlite3_exec(database, script.c_str(), nullptr, nullptr, nullptr);
//open the rooms //open the rooms
cout << "DEBUG: inserting the room..." << endl; worldRoomMap.insert( pair<int, WorldRoom*>(worldRoomCounter++, new WorldRoom(playerMap)));
worldRoomMap.insert( pair<int, WorldRoom>(worldRoomCounter++, WorldRoom(playerMap)));
cout << "DEBUG: opening the rooms..." << endl;
for (auto& it : worldRoomMap) { for (auto& it : worldRoomMap) {
it.second.OpenRoom(); it.second->OpenRoom();
} }
cout << "DEBUG: Finished initialization" << endl;
} }
void ServerApplication::Loop() { void ServerApplication::Loop() {
//debugging
NetworkPacket packet; NetworkPacket packet;
while(running) { while(running) {
@@ -127,7 +121,8 @@ void ServerApplication::Loop() {
void ServerApplication::Quit() { void ServerApplication::Quit() {
//close the rooms //close the rooms
for (auto& it : worldRoomMap) { for (auto& it : worldRoomMap) {
it.second.CloseRoom(); it.second->CloseRoom();
delete it.second;
} }
worldRoomMap.clear(); worldRoomMap.clear();
@@ -143,7 +138,7 @@ void ServerApplication::Quit() {
void ServerApplication::HandlePacket(NetworkPacket packet) { void ServerApplication::HandlePacket(NetworkPacket packet) {
//debgging //debgging
for (auto& it : worldRoomMap) { for (auto& it : worldRoomMap) {
it.second.GetInQueue()->PushBack(packet); it.second->GetInQueue()->PushBack(packet);
} }
switch(packet.meta.type) { switch(packet.meta.type) {
+1 -1
View File
@@ -82,7 +82,7 @@ private:
//global lists //global lists
ClientMap clientMap; ClientMap clientMap;
PlayerMap playerMap; PlayerMap playerMap;
WorldRoomMap worldRoomMap; std::map<int, WorldRoom*> worldRoomMap;
int clientCounter = 0; int clientCounter = 0;
int playerCounter = 0; int playerCounter = 0;
-11
View File
@@ -27,22 +27,16 @@
using namespace std; using namespace std;
int worldRoomThread(void* arg) { int worldRoomThread(void* arg) {
cout << "DEBUG: in room thread" << endl;
WorldRoom* room = reinterpret_cast<WorldRoom*>(arg); WorldRoom* room = reinterpret_cast<WorldRoom*>(arg);
cout << "DEBUG: Beginning try block" << endl;
try { try {
cout << "DEBUG: Init" << endl;
room->Init(); room->Init();
cout << "DEBUG: Loop" << endl;
room->Loop(); room->Loop();
cout << "DEBUG: Quit" << endl;
room->Quit(); room->Quit();
} }
catch(exception& e) { catch(exception& e) {
cerr << "Fatal room error: " << e.what() << endl; cerr << "Fatal room error: " << e.what() << endl;
return 1; return 1;
} }
cout << "DEBUG: Successfully ending room thread" << endl;
return 0; return 0;
} }
@@ -57,18 +51,15 @@ WorldRoom::~WorldRoom() {
} }
void WorldRoom::OpenRoom() { void WorldRoom::OpenRoom() {
cout << "DEBUG: In OpenRoom" << endl;
if (running) { if (running) {
throw(std::runtime_error("Cannot open a room that is already running")); throw(std::runtime_error("Cannot open a room that is already running"));
} }
running = true; running = true;
cout << "DEBUG: Attempting to create thread" << endl;
if (!(thread = SDL_CreateThread(worldRoomThread, this))) { if (!(thread = SDL_CreateThread(worldRoomThread, this))) {
throw(std::runtime_error("Failed to open the room thread")); throw(std::runtime_error("Failed to open the room thread"));
} }
cout << "DEBUG: End of thread call" << endl;
} }
void WorldRoom::CloseRoom() { void WorldRoom::CloseRoom() {
@@ -86,9 +77,7 @@ void WorldRoom::Init() {
} }
void WorldRoom::Loop() { void WorldRoom::Loop() {
cout << "DEBUG: In Loop" << endl;
while(running) { while(running) {
cout << "DEBUG: Top of loop" << endl;
while(networkInQueue.Size() > 0) { while(networkInQueue.Size() > 0) {
HandlePacket(networkInQueue.PopFront()); HandlePacket(networkInQueue.PopFront());
} }
-2
View File
@@ -60,6 +60,4 @@ private:
PlayerMap playerMap; PlayerMap playerMap;
}; };
typedef std::map<int, WorldRoom> WorldRoomMap;
#endif #endif