Hello, I'm kind of new to C++ development in linux and I'm trying to make a multiplayer game. I know that it is a bit of complex program to start but I have some background on this type of program from other languages so I guess the most difficult part is taming the language.

Although I'm programming a multiplayer game, my doubts are about the best way to handle the memory and avoid leaks in C++.

My doubts are about allocating memory for the client objects and for the game tables. For the client objects I've read that the std containers handle the memory allocation for me. I don't know if this memory is allocated on heap so I've decided to use a map of pointers (with the socket fd as key) to client object. This way, I have something like this when a client connect and disconnect:

Daemon.cpp

map<int,Client*> clientList;

//Do server stuff

//Add connected client to list
void onConnect(int socketFd) {
clientList[socketFd] = new Client();
}

//remove connected client from list
void onDisconnect(int socketFd) {
delete clientList[socketFd];
clientList.erase(socketFd);
}

The Client class is a simple class that has a virtual destructor, some client parameters (like IP, connected time, etc) and some methods (like send, etc). Is this the best way to keep track of clients without memory problems? I guess I still have to add exceptions handling on new Client() allocations...

The second part, and I guess the most difficult for me, is about the game tables. Clients can enter and leave tables of games. I have a table class with a lot of parameters, constants and methods. I'm creating all the game tables on start up on the same Daemon.cpp described above:

Daemon.cpp

GameTable *tables;

int main() {
tables = new Chess[MAX_NUMBER_OF_TABLES];
}

Some explanations: GameTable is the base class for all games. It is an interface with base parameters and virtual game functions (like doCommand, addClient, removeClient, etc). Chess class is the implementation of the chess game, it inheritance (sorry bad english) from GameTable. Questions:

1) Is this the best way (memory) to handle it?
2) Chess class has a lot of parameters, when I allocate the table list of Chess objects do the memory for all objects already allocated or I have to allocate and dealocate inside Chess class (with constructors and destructors)?

My third question is how to add and remove clients to/from tables. First I thought in creating a simple vector with clients like:

GameTable.h

vector <Client> clientInTable;


Chess.cpp

//Add client to table
void addClient(Client &client) {
clientInList.push_back(client);
}

//remove client from table
void removeClient(Client &client) {
//search client on list, when found get position pos
clientList.erase(pos);
}

Soon I've noticed that when I remove the client its destructor was called. It must not happen! Than I thought in use a vector of pointers like:

GameTable.h

vector <Client*> clientInTable;


Chess.cpp
//Add client to table
void addClient(Client *client) {
clientInList.push_back(client);
}

//remove client from table
void removeClient(Client *client) {
//search client on list, when found get position pos
clientList[pos] = NULL;
}

Is this the best way to handle it? Thanks everybody for the help.