Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: removing a socket from list on disconnect  (Read 6219 times)

0 Members and 1 Guest are viewing this topic.

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
removing a socket from list on disconnect
« on: February 07, 2013, 08:57:42 pm »
Based on the example from the SocketSelector class reference in the SFML 2.0 documentation i completed the code to remove a TcpSocket from the std::list after it disconnects like this:

                for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
                {
                    sf::TcpSocket& client = **it;
                    if (selector.isReady(client))
                    {
                        sf::Packet packet;
                        if (client.receive(packet) == sf::Socket::Done)
                        {
                            std::cout<<"Done"<<std::endl;
                        }
                        if (client.receive(packet) == sf::Socket::Disconnected)
                        {
                            std::cout<<"Client disconnected"<<std::endl;
                            std::cout<<"list size: "<<clients.size();
                            selector.remove(client);
                            client.disconnect();
                            delete(&client);
                            clients.erase(it);
                            it--;
                            std::cout<<" then: "<<clients.size()<<std::endl;
                        }
                    }
                }
 

It is working great but i get memory leak and some other non critical errors when i profile it with Valgrind
how could i enhance it more ?
« Last Edit: February 07, 2013, 09:06:27 pm by wbaccari »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: removing a socket from list on disconnect
« Reply #1 on: February 07, 2013, 09:29:29 pm »
Quote
i get memory leak and some other non critical errors when i profile it with Valgrind
Which errors?
Laurent Gomila - SFML developer

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: removing a socket from list on disconnect
« Reply #2 on: February 07, 2013, 09:49:12 pm »
==5638== HEAP SUMMARY:
==5638==     in use at exit: 13,051,799 bytes in 1,173 blocks
==5638==   total heap usage: 169,543 allocs, 168,370 frees, 1,477,238,691 bytes allocated
==5638==
==5638== LEAK SUMMARY:
==5638==    definitely lost: 172 bytes in 1 blocks
==5638==    indirectly lost: 2,424 bytes in 12 blocks
==5638==      possibly lost: 13,021,927 bytes in 742 blocks
==5638==    still reachable: 27,276 bytes in 418 blocks
==5638==         suppressed: 0 bytes in 0 blocks
==5638== Rerun with --leak-check=full to see details of leaked memory
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: removing a socket from list on disconnect
« Reply #3 on: February 07, 2013, 09:59:22 pm »
This is the summary, we need to see the detailed report.
Laurent Gomila - SFML developer

alexyoung91

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: removing a socket from list on disconnect
« Reply #4 on: May 23, 2013, 12:51:40 am »
I'm sorry to bring this up again but is this the correct/best way to disconnect a client and remove a socket from the list?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: removing a socket from list on disconnect
« Reply #5 on: May 23, 2013, 08:09:08 am »
This code is from the API documentation, so... yes.
Laurent Gomila - SFML developer