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

Author Topic: Network 'bad allocation' problem  (Read 2633 times)

0 Members and 1 Guest are viewing this topic.

hoked12

  • Newbie
  • *
  • Posts: 3
    • View Profile
Network 'bad allocation' problem
« on: May 24, 2017, 01:50:57 pm »
Hello, i recently started to learn SFML networking. I'm trying to create a client-server communication.
When i try to send a packet from the client, it says 'bad allocation'. I don't understand why i'm getting this error, thanks in advance.
Here's the code:

Before main:
class ClientT {
public:
        int pp;
        std::string name;
        int frm;
private:
};

sf::Packet& operator << (sf::Packet& packet, const ClientT& character) {
        return packet << character.name << character.pp << character.frm;
}

sf::Packet& operator >> (sf::Packet& packet, ClientT& character) {
        return packet >> character.name >> character.pp >> character.frm;
}

On main:
        std::vector<sf::TcpSocket*> clients;

        char choice;
        std::cin >> choice;

        if (choice == 'S') {
                sf::SocketSelector selector;
                sf::TcpListener listener;
                listener.listen(5000);
        selector.add(listener);

                while (1) {
                        if (selector.wait()) {
                                if (selector.isReady(listener)) {
                                        sf::TcpSocket* socket = new sf::TcpSocket;
                                        if (listener.accept(*socket) == sf::Socket::Done) {

                                                std::vector<sf::TcpSocket*>::iterator it = clients.begin();
                                                for (it; it != clients.end(); ++it) {
                                                        sf::TcpSocket* sockett = *it;
                                                        char in[128] = "A client connected!";
                                                        sockett->send(in, sizeof(in));
                                                }

                                                clients.push_back(socket);
                                                selector.add(*socket);
                                        }
                                        else {
                                                delete socket;
                                        }
                                }
                                else {
                                        std::vector<sf::TcpSocket*>::iterator it = clients.begin();
                                        for (it; it != clients.end(); ++it) {
                                                sf::TcpSocket* socket = *it;
                                        }
                                }
                        }
                }
       
        }
        else if (choice == 'C') {
                sf::IpAddress address;
                std::cout << "Enter the host's IP: ";
                std::cin >> address;

                sf::TcpSocket* socket = new sf::TcpSocket;
                if (socket->connect(address, 5000) == sf::Socket::Done) {
                        std::cout << "Connected" << std::endl;
                }

                ClientT client;
                client.name = "hoked12";
                client.pp = 1;
                client.frm = 0;

                sf::Packet packet;
                try {
                        packet << client;
                }
                catch (std::exception& e) {
                        std::cout << e.what() << std::endl;
                }

                // if (socket.send(packet) == sf::Socket::Done) std::cout << "Packet sent" << std::endl;
        }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Network 'bad allocation' problem
« Reply #1 on: May 24, 2017, 02:18:40 pm »
Start up a debugger and get a stack trace of the crash and use it to check the states of various variables.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hoked12

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Network 'bad allocation' problem
« Reply #2 on: May 24, 2017, 02:49:07 pm »
Start up a debugger and get a stack trace of the crash and use it to check the states of various variables.

I tried to put a breakpoint and check the actual values of the variables, they are all fine, but when i check the info about the packet, this is what i get:

packet
<Information not available, no symbols loaded for sfml-network-2.dll>
    __vfptr: sfml-network-2.dll!0x5c6c36d8 (load symbols for additional information) {sfml-network-2.dll!0x5c6be100, ...}
    m_data: { size=0 }
    m_readPos: 0
    m_sendPos: 3435973633
    m_isValid: true (204)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Network 'bad allocation' problem
« Reply #3 on: May 24, 2017, 04:05:56 pm »
The debugger does a much better job in debug mode ;) (sfml-network-2.dll is a release DLL, which shows that your project was not linked against the SFML debug libraries, and thus was probably compiled in release too).
Laurent Gomila - SFML developer

hoked12

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Network 'bad allocation' problem
« Reply #4 on: May 25, 2017, 06:49:49 pm »
The debugger does a much better job in debug mode ;) (sfml-network-2.dll is a release DLL, which shows that your project was not linked against the SFML debug libraries, and thus was probably compiled in release too).

Thank you, i've tried that and i found the problem. Apparently the order of the libraries were messed up. Now i can send and receive packets correctly.

 

anything