Help with memory allocation for multiplayer game server
<p>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.</p>

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

<p>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:</p>

<p>Daemon.cpp</p>

<pre><code>map&lt;int,Client*&gt; 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);
}
</code></pre>

<p>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...</p>

<p>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:</p>

<p>Daemon.cpp</p>

<pre><code>GameTable *tables;

int main() {
tables = new Chess[MAX_NUMBER_OF_TABLES];
}
</code></pre>

<p>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:</p>

<p>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)?</p>

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

<p>GameTable.h</p>

<pre><code>vector &lt;Client&gt; clientInTable;
</code></pre>

<p>Chess.cpp</p>

<pre><code>//Add client to table
void addClient(Client &amp;client) {
clientInList.push_back(client);
}

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

<p>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:</p>

<p>GameTable.h</p>

<pre><code>vector &lt;Client*&gt; clientInTable;
</code></pre>

<p>Chess.cpp</p>

<pre><code>//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;
}
</code></pre>

<p>Is this the best way to handle it? Thanks everybody for the help. </p>
