Accessing array pointer in Header file?

1

By attempting to access the TCPSocket inside my "clientArray" I get a Access Violation error. How would I access it properly?

My header file holds the TCPSocket *clientArray.

public:
TCPsocket *clientArray;
SDLNet_SocketSet aSocketSet;
bool serverOn;

It is defined within my constructor:

clientArray = new TCPsocket[maxsockets];
aSocketSet = SDLNet_AllocSocketSet(maxsockets);

It is accessible within another function of mine (it works here without issue):

void ServerSocket::waitingForConnection() {

    std::cout << '\r' << flush << "Players connected: " << playersConnected        << ". Listening for connection...  ";
    TCPsocket newsocket = SDLNet_TCP_Accept(serverSocket);
    SDL_Delay(1000);

    if (!newsocket){
        //std::cout << '\r' << flush <<  "Listening for connection.  ";
        //std::cout << SDLNet_GetError() << std::endl;
    }

    else{
        std::cout << '\r' << flush <<  "Socket (client " << slotnum + 1 << ") created successfully.                           " << std::endl;
        clientArray[slotnum] = newsocket;
        int n = SDLNet_TCP_AddSocket(aSocketSet, newsocket);

        if (n < 0){
            std::cout << "Client " << slotnum + 1 << " failed to connect. " << std::endl;
        }
        else{
            char text[10];
            std::cout << "Client " << slotnum + 1 << " added to client array successfully." << std::endl;

            serverMessage(slotnum, "2 You are successfully connected to the server.");
            std::cout << "Sent connection validation to Client " << slotnum + 1 << "." << endl;

            std::cout << "Allocating player " << slotnum + 1 << " with player number ." << endl;

            serverData(slotnum, '5', slotnum+1);

            //ACCESSING IT HERE WITHOUT ISSUE
            SDLNet_TCP_Recv(clientArray[slotnum], text, 10);
            std::cout << "received text = " << text << endl;
            interpretData(text);
            slotnum++;
         }

        //SDLNet_TCP_Close(newsocket);
        //SDLNet_TCP_Close(serverSocket);
        //code here
    }
}

However later on when I try to access it via another function, I get an Access Violation Error :

Unhandled exception at 0x00AED839 in Server.exe: 0xC0000005: Access violation reading location 0x0000000C.

I am calling the problematic function from my Game's Update function as following:

void Game::Update(){
   while (g_playersConnected == 2)
   {
        printGrid();
        serverSocket->waitForPlayer((playerTurn-1));
        changeTurn();
        system("pause");
   }
    //cout << "Game's Update is running" << endl;
};

This is my other function that is attempting to access the array :

void ServerSocket::waitForPlayer(int playerNum)
{
    cout << "Waiting for player " << playerNum + 1 << " (In array : " <<     playerNum << ")." << endl;
    char text[10];
    SDLNet_TCP_Recv(clientArray[playerNum], text, 10);
    std::cout << "received text = " << text << endl;
    interpretData(text);
}

I have not set up Copy constructors or assignment operators and my destructors are just empty blocks at the moment.

 ServerSocket::~ServerSocket(){}

Which direction should I go towards solving this issue?

All the best

c++
networking
sdl
sdl-net
asked on Stack Overflow Apr 15, 2016 by Antsuw • edited Apr 15, 2016 by Antsuw

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0