0

所以我试图整齐地设置我的代码做我通常做的事情,但现在我在运行时遇到这个错误

First-chance exception at 0x01313D46 in Server.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

Visual Studio 还在 locals 框中告诉我它无法读取内存

unsigned short port
bool listening

这是发生错误的标头和 Cpp

#pragma once

#include <SFML\Network.hpp>
#include <iostream>
using namespace std;

class NetServer
{
private:
    bool listening;
    unsigned short port;
public:
    NetServer(void);
    ~NetServer(void);

    void InitNetServer();

    void SetListening(bool a)
    {
        a = listening;
    }
    bool IsListening()
    {
        return listening;
    }

    void SetPort(unsigned short a)
    {
        a = port;
    }
    int GetPort()
    {
        return port;
    }
};

当它变得太 SetListening(bool a) 时调试会中断,然后如果我把它拿出来,它会在 SetPort 中断

这是我的cp

#include "NetServer.h"

sf::TcpListener listener;
sf::TcpSocket client;

NetServer::NetServer(void)
{
    //listener.listen(21025);
    //cout << "listening" << endl;
    while (true)
    {
        if (listener.accept(client) == sf::Socket::Done)
        {
            cout << "New connection received from " << client.getRemoteAddress() << endl;
        }
    }
}

NetServer::~NetServer(void)
{
    SetPort(NULL);
    SetListening(false);
}

void NetServer::InitNetServer()
{
    cout << "Initialising the NetServer" << endl;
    SetListening(true);
    SetPort(45000);

    listener.listen(GetPort());
    cout << "NetServer Initialised : Listening on Port "<< GetPort() << endl;
}

这是我唯一的另一个类,它也在 GameServer.h/cpp 中调用

    #include "GameServer.h"

GameServer::GameServer()
{
    Run();
}

GameServer::~GameServer(void)
{
    DestroyNetServer();
    Exit();
}

void GameServer::Run()
{
    cout << "Initialising the engine." << endl;

    /* Initialise stuff here */
    _NetServer->InitNetServer();
    /* Finish initialisation */

    SetGameRunning(true);

    while(GetGameRunning() == true)
    {
        _NetServer;
    }
}

void GameServer::Exit()
{
    exit(0);
}

NetServer & GameServer::GetNetServer()
{
    if(_NetServer == 0) throw exception ("Null Game Server");
    return *_NetServer;
}

void GameServer::CreateNetServer()
{
    _NetServer = new NetServer();
}

void GameServer::DestroyNetServer()
{
    delete _NetServer;
}




#pragma once

#include "NetServer.h"

#include <iostream>
using namespace std;

class NetServer;

class GameServer
{
public:
    GameServer();
    ~GameServer(void);

    void Run();
    void Exit();

    void SetGameRunning(bool running)
    {
        gameRunning = running;
    }
    bool GetGameRunning()
    {
        return gameRunning;
    }

    void CreateNetServer();
    NetServer & GetNetServer();
    void DestroyNetServer();

private:
    bool gameRunning;

    NetServer * _NetServer;
};
4

2 回答 2

0

It looks like you never initialize listening or port. Even your setter methods won't help you, because the assignment is flipped around (you're assigning values to the local parameter, rather than copying the value to your instance member).

For example:

void SetListening(bool a)
{
    a = listening;
}

Should probably be:

void SetListening(bool a)
{
    listening = a;
}

Besides fixing your setters, to fix the problem of never assigning listening or port values, you need to do that in the constructor initialization list:

NetServer::NetServer(void)
  : listening(false)
  , port(0)
{
    //listener.listen(21025);
    //cout << "listening" << endl;
    while (true)
    {
        if (listener.accept(client) == sf::Socket::Done)
        {
            cout << "New connection received from " << client.getRemoteAddress() << endl;
        }
    }
}

Also, I have no idea what the intent of this code even is, but it looks wrong:

while(GetGameRunning() == true)
{
    _NetServer;
}
于 2013-09-12T07:49:19.873 回答
0

在构造函数中调用 Run 并且没有人初始化 _NetServer 指针。所以你试图调用一个未启动对象的方法。在调用 Run() 之前添加 CreateNetServer

GameServer::GameServer()
{
  CreateNetServer()
  Run();
}
于 2013-09-08T22:59:17.153 回答