SFML community forums

Help => General => Topic started by: Soul on March 02, 2013, 01:53:00 am

Title: [SOLVED]pointer erase std::vector
Post by: Soul on March 02, 2013, 01:53:00 am
hey, when i want erase pointer server stop working...

void Server::Recv()
{
    int id = 0;
    for ( std::vector<Client*>::iterator it = clients.begin(); it != clients.end(); ++it)
    {
        id++;
        Client &client = **it;
        client.ClientID = id;
        if ( Selector.isReady( client.Socket ) )
        {
            if ( client.Socket.receive( client.Recv ) == sf::Socket::Done )
            {
                std::string str, recv;
                std::ostringstream ss;
                client.Recv >> str;
                ss << client.ClientID;
                recv = "Received from ID: " + ss.str();
                recv += " " + str;
                Console::Out( recv.c_str(), str.c_str() );
                client.Recv >> str;
                if( str == "Bye" )
                {
                    Selector.remove( client.Socket );
                    str = "Client: " + ss.str() + " disconnected";
                    Console::Out( str.c_str() );
                    clients.erase( it );      // there
                }
            }
        }
    }
}

class Client
{
    public:
        sf::TcpSocket Socket;
        sf::Packet Send;
        sf::Packet Recv;
        int ClientID;
};
 

any ideas? : /
Title: Re: pointer erase std::vector
Post by: G. on March 02, 2013, 03:11:55 am
Related (http://stackoverflow.com/questions/4645705/vector-erase-iterator)?

If not, what's "stop working"?
Title: Re: pointer erase std::vector
Post by: Soul on March 02, 2013, 10:05:38 am
yup, when i add return; after clients.erase(it); program work fine
thanks for help