访问冲突读取位置0xCCCCCCCC.using bool&未签约的短片

时间:2013-09-08 21:18:00

标签: c++ oop sfml

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

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

Visual Studio还在本地框中告诉我它无法读取

的内存
unsigned short port
bool listening

这是发生错误的Header和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中断开

这是我的Cpp

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

2 个答案:

答案 0 :(得分:0)

在构造函数中调用Run,没有人正在初始化_NetServer指针。 所以你试图调用一个未初始化对象的方法。 在调用Run()

之前添加CreateNetServer
GameServer::GameServer()
{
  CreateNetServer()
  Run();
}

答案 1 :(得分:0)

您似乎永远不会初始化listeningport。即使你的setter方法也无法帮助你,因为转换了分配(你将值赋给local参数,而不是将值复制到你的实例成员)。

例如:

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

应该是:

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

除了修复您的setter之外,要解决永不分配listeningport值的问题,您需要在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;
        }
    }
}

另外,我不知道这个代码的意图是什么,但看起来不对:

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