简体   繁体   English

c ++ vector访问冲突读取位置

[英]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. 当我尝试在函数中向我的向量中推送元素时,我得到一个Access违规读取位置,但如果我在类构造函数中执行它,它会工作。

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 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); 如果我执行命令: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. 在类构造函数中,它可以工作,我可以在调试器中看到它,但是当我到达ServerAcceptsConnection()函数时,它确实包含了一些元素。 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. 如果我尝试在那里添加somthing我得到此错误:server.exe中0x00c730ab处的未处理异常:0xC0000005:访问冲突读取位置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(); while(true)循环中,您正在调用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). 函数,在每次调用时构造一个SOCKET对象,然后将其推送到m_clientList ,并且它无限期地执行此操作(因为while无限期地运行)。 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; 我在标题SOCKET m_client中发现了问题; goos out of bounderys if i put the vector above this line it works 如果我把矢量放在这条线以上就可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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