CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2010
    Posts
    1

    Help with memory allocation for multiplayer game server

    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.

  2. #2
    Join Date
    Jul 2009
    Posts
    37

    Re: Help with memory allocation for multiplayer game server

    What you have provided here has nothing special, please specify some more
    Empty logic is like tissues people don't need even for wiping porblems, this needs AI techniques inserted to fish or hook the the user(s)
    Sig-na-tju-(r)

  3. #3
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Help with memory allocation for multiplayer game server

    Quote Originally Posted by Akirasan View Post
    my doubts are about the best way to handle the memory and avoid leaks in C++.
    As long as you don't create objects using 'new', there's no need to worry about memory leaks.
    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
    STL containers use std::allocator class as the default allocator which in turn probablly uses operator new/delete internally. These should not be any concerns to the users for they are implementation details.
    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...
    What memory problem are you referring to?
    1) Is this the best way (memory) to handle it?
    what do you mean?
    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)?
    Please refer to my explanation on STL containers above.
    My third question is how to add and remove clients to/from tables.
    Your approach is fine, although you might want to look up some of the functions in <algorithm> and how they can be used with the container.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured