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

Author Topic: Unhandled exception: Access violation  (Read 4774 times)

0 Members and 1 Guest are viewing this topic.

A13X

  • Newbie
  • *
  • Posts: 3
    • View Profile
Unhandled exception: Access violation
« 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;
}
 
« Last Edit: July 23, 2013, 11:17:26 pm by A13X »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Unhandled exception: Access violation
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

A13X

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Unhandled exception: Access violation
« Reply #2 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

Atani

  • Newbie
  • *
  • Posts: 30
    • MSN Messenger - atanisoft@hotmail.com
    • AOL Instant Messenger - AtaniSYSOP
    • Yahoo Instant Messenger - atanisoft
    • View Profile
    • Email
Re: Unhandled exception: Access violation
« Reply #3 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.

A13X

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Unhandled exception: Access violation
« Reply #4 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Unhandled exception: Access violation
« Reply #5 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/