简体   繁体   中英

c++ vector Access violation reading location

I get a Access violation reading location when i try to push a element in my vector in a function but if i do it in the class constructor it works.

header file of the class that gives problems

#include <winsock2.h>
#include <string>
#include <vector>

#pragma comment(lib,"ws2_32.lib") //Winsock Library


class Network{
    public:

    /*
    * General functions
    */
    Network();
    Network(bool debugModus);

    ~Network();

    /*
    * Server Functions
    */
    int ServerSetup(int port);
    void ServerAcceptConnections();

    /* 
    * Server variable
    */
    SOCKET m_client;
    std::vector<SOCKET> m_clientList;

};

the cpp

void Network::ServerAcceptConnections()
{
     SOCKET new_socket;
     m_clientList.push_back(new_socket);
}

the main

void main ()
{
    Network network (true);
    network.ServerSetup(800);
    while(true)
    {
        network.ServerAcceptConnections();
    }

}

If i do the command: m_clientList.push_back(new_socket); In the class constructor it works and i can see it in the debugger, but when I reach the ServerAcceptsConnection() function it doenst contain anny elements. and if i try to add somthing there I get this error: Unhandled exception at 0x00c730ab in server.exe: 0xC0000005: Access violation reading location 0x01000083.

edit the whole code the header

    #include <winsock2.h>
    #include <string>
    #include <vector>

    #pragma comment(lib,"ws2_32.lib") //Winsock Library

    struct Message{
        char* message;
        unsigned int max_size;
        unsigned int size;
    };

    class Network{
        public:

        /*
        * General functions
        */
        Network();
        Network(bool debugModus);
        int Connect(std::string ipAddress, int port);
        int Send(SOCKET socket, char* message, unsigned int size);
        Message Receive(SOCKET socket, int size = 2000);

        ~Network();

        /*
        * Server Functions
        */
        int ServerSetup(int port);
        void ServerAcceptConnections();

        private:
        /*
        * General Functions
        */
        int Init();

        /*
        * Server functions
        */
        int Bind(int port);
        int Listen();

        /*
        * General Variable
        */
        bool m_debugModus;
        WSADATA m_wsa;
        SOCKET m_s;
        struct sockaddr_in m_server;

        /* 
        * Server variable
        */
        SOCKET m_client;
        std::vector<SOCKET> m_clientList;
        std::vector<Message> m_outBuffer;
        std::vector<Message> m_inBuffer;
    };

the cpp

#include "NetworkClass.h"
#include<stdio.h>

Network::Network()
{
    m_debugModus = false;
    Init();
}
Network::Network(bool debugModus)
{
    m_debugModus = true;
    if(m_debugModus) printf("\nDEBUG MODUS!!");
    if(m_debugModus) printf("\nThis mode is for debugging only!!");
    if(m_debugModus) printf("\n");
    if(m_debugModus) printf("\n");
    Init();
}

int Network::Init()
{
    SOCKET new_socket;
    m_clientList.push_back(new_socket);
    if(m_debugModus) printf("\nDEBUG MODUS: Initialising Winsock...");

    if (WSAStartup(MAKEWORD(2,2),&m_wsa) != 0)
    {
        if(m_debugModus) printf("DEBUG MODUS: Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    if(m_debugModus) printf(" Initialised.\n");

    /* 
    * creating a socket
    *
    * Settings:
    * Address Family : AF_INET (this is IP version 4)
    * Type : SOCK_STREAM (this means connection oriented TCP protocol)
    * Protocol : 0 [ or IPPROTO_TCP , IPPROTO_UDP ]
    */
    if((m_s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        if(m_debugModus) printf("DEBUG MODUS: Could not create socket : %d" , WSAGetLastError());
    }

    if(m_debugModus) printf("DEBUG MODUS: Socket created.\n");
    return 0;
}

int Network::Connect(std::string ipAddress, int port)
{
    if(m_debugModus) printf("DEBUG MODUS: Trying to connect with: %s on port %d \n", ipAddress.c_str(), port);
    m_server.sin_addr.s_addr = inet_addr(ipAddress.c_str());
    m_server.sin_family = AF_INET;
    m_server.sin_port = htons( port );

    if (connect(m_s , (struct sockaddr *)&m_server , sizeof(m_server)) < 0)
    {
        if(m_debugModus) printf("DEBUG MODUS: Error cant open a connecting\n", ipAddress, port);
        return 1;
    }
    if(m_debugModus) printf("DEBUG MODUS: Connected\n");
    return 0;
}

int Network::Send(SOCKET socket, char* message, unsigned int size)
{
    if( send(socket , message , size , 0) < 0)
    {
        if(m_debugModus) printf("DEBUG MODUS: Send failed error: %d\n", WSAGetLastError());
        return 1;
    }
    if(m_debugModus) printf("DEBUG MODUS: Data Send\n");
    return 0;
}

Message Network::Receive(SOCKET socket, int size)
{
    Message message;
    message.message = (char*) malloc(sizeof(char)*size);
    message.max_size = size;
    if((message.size = recv(socket , message.message , message.max_size , 0)) == SOCKET_ERROR)
    {
        if(m_debugModus) printf("DEBUG MODUS: recv failed");
    }

    if(m_debugModus) printf("DEBUG MODUS: Reply received\n");

    return message;
}

int Network::Bind(int port)
{
    m_server.sin_family = AF_INET;
    m_server.sin_addr.s_addr = INADDR_ANY;
    m_server.sin_port = htons( port );

    if( bind(m_s ,(struct sockaddr *)&m_server , sizeof(m_server)) == SOCKET_ERROR)
    {
        if(m_debugModus) printf("DEBUG MODUS: Bind failed with error code : %d\n" , WSAGetLastError());
        return 1;
    }

    if(m_debugModus) printf("DEBUG MODUS: Bind done\n");
    return 0;
}

int Network::Listen()
{
    listen(m_s , 3);
    return 0;
}

int Network::ServerSetup(int port)
{
    Bind(port);
    Listen();
    return 0;
}

void Network::ServerAcceptConnections()
{
    SOCKET new_socket;
    int c = sizeof(struct sockaddr_in);
    new_socket = accept(m_s , (struct sockaddr *)&m_client, &c);
    if (new_socket != INVALID_SOCKET )
    {
        if(m_debugModus) printf("DEBUG MODUS: Connection accepted\n");

        //Reply to the client
        Send(new_socket, "Hello Client , I have received your connection. But I have to go now, bye\n", strlen("Hello Client , I have received your connection. But I have to go now, bye\n"));
        m_clientList.push_back(new_socket);
    }

    if (new_socket == INVALID_SOCKET)
    {
        if(m_debugModus) printf("DEBUG MODUS: accept failed with error code : %d" , WSAGetLastError());
    }
}

Network::~Network()
{
    closesocket(m_s);
    WSACleanup();
}

It looks like you are exceeding the capacity of the vector. In the while(true) loop, you are invoking network.ServerAcceptConnections(); function, which at each invocation constructs a SOCKET object, then pushes it into m_clientList , and it does this indefinitely (as the while runs indefinitely). At least this is what seems to happen looking at your code snipped.

On the other hand, the class constructor is invoked only once.

i found the problem in the header the SOCKET m_client; goos out of bounderys if i put the vector above this line it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM