SFML community forums

Help => Network => Topic started by: A13X on July 23, 2013, 11:14:11 pm

Title: Unhandled exception: Access violation
Post by: A13X on July 23, 2013, 11:14:11 pm
Hi,
I've made a little chatprogramm(just for fun ;) ) and I have this little problem you can see in the title.

the full error message is
"Unhandled exception at 0x773f15de in sfmlserver.exe: 0xC0000005: Access violation."
It pops up everytime my client is connecting to my server (both on my computer).

Actually I don't even know if the connection causes the problem.
code of the server:
#include<SFML\Audio.hpp>
#include<SFML\Graphics.hpp>
#include<SFML\Network.hpp>
#include<iostream>
#include<string>
#include<vector>
#include<conio.h>
int main()
{
        std::cout<<"server is online"<<std::endl;
        sf::TcpListener list;
        sf::SocketSelector select;
        std::vector<sf::TcpSocket*> clients;
        list.listen(50000);
        select.add(list);
        while(true){
                if(select.wait()){
                        if(select.isReady(list)){
                                sf::TcpSocket *socket = new sf::TcpSocket;
                                list.accept(*socket);
                                sf::Packet packet;
                                std::string id;
                                if(socket->receive(packet)==sf::Socket::Done)
                                                packet>>id;

                                std::cout<< id << " has joined the chatroom" << std::endl;
                                clients.push_back(socket);
                                select.add(*socket);
                        }
                        else{
                                for(int i=0;i<clients.size();i++){
                               
                                        if(select.isReady(*clients[i])){
                                                sf::Packet pack;
                                                if(clients[i]->receive(pack)==sf::Socket::Done){
                                                        std::string text;
                                                        for(int j=0;i<clients.size();j++){
                                                                if(j!=i){
                                                                        clients[j]->send(pack);
                                                                }      
                                                        }
                                                }
                                        }
                                }
                        }
                }
        for(std::vector<sf::TcpSocket*>::iterator it=clients.begin(); it!=clients.end();it++)
                delete *it;
        }
        system("pause");
        return 0;
}
 
Title: Re: Unhandled exception: Access violation
Post by: eXpl0it3r on July 23, 2013, 11:40:11 pm
Have you ever heard of the magical tool debugger? Try to run it through that and find out what causes the problem.
Also you shouldn't use manual memory management, but rather make use of RAII and smart pointers. ;)
Title: Re: Unhandled exception: Access violation
Post by: A13X on July 23, 2013, 11:54:02 pm
Have you ever heard of the magical tool debugger? Try to run it through that and find out what causes the problem.
1. I'm using Visual Studio. It has a debugger. But it sayed nothing helpful.
just
Quote from: VC++Debugger
Access violation when reading at position 0xFEEEFEEE.
!

Also you shouldn't use manual memory management, but rather make use of RAII and smart pointers. ;)
2. I think manual memory management is okay for the moment
Title: Re: Unhandled exception: Access violation
Post by: Atani on July 24, 2013, 12:01:26 am
the exception you are seeing is very likely related to using a pointer you have already freed but still trying to use.

You probably want to clear the selector before you start deleting the tcp sockets, also closing them if they are open would be recommended.

If you run this within VS in debug mode it should trigger the debugger and tell you which line is causing the exception.
Title: Re: Unhandled exception: Access violation
Post by: A13X on July 24, 2013, 12:11:58 am
Oops!
Found it. The last for-loop deleting all the sockets, was in the while loop which uses the sockets. This is truly wrong
Title: Re: Unhandled exception: Access violation
Post by: eXpl0it3r on July 24, 2013, 01:21:14 am
2. I think manual memory management is okay for the moment
Oh really? :D

Oops!
Found it. The last for-loop deleting all the sockets, was in the while loop which uses the sockets. This is truly wrong