访问冲突读取位置

访问冲突读取位置

当我尝试在函数中将向量中的元素推入时会出现访问冲突读取位置,但是如果我在类构造函数中执行此操作,它将起作用。

产生问题的类的头文件

#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;

};


cpp

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


主要的

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

}


如果我执行命令:
m_clientList.push_back(new_socket);
在类构造函数中,它可以工作,我可以在调试器中看到它,
但是当我到达ServerAcceptsConnection()函数时,它确实包含anny元素。
如果我尝试在此添加东西,则会出现此错误:
server.exe中0x00c730ab处未处理的异常:0xC0000005:访问冲突读取位置0x01000083。

编辑整个代码
标头

    #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();
}

最佳答案

看来您超出了向量的容量。在while(true)循环中,您要调用network.ServerAcceptConnections();函数,该函数在每次调用时都会构造一个SOCKET对象,然后将其推入m_clientList,并且它会无限期地执行此操作(因为while无限期地运行)。至少在您的代码被截断时,这似乎发生了。

另一方面,类构造函数仅被调用一次。

关于c++ - C++ vector 访问冲突读取位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23714189/

10-11 19:00