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

Antsuw
  • 37
  • 5
  • 1
    did you check that `playerNum` is smaller than `maxsockets` ? – 463035818_is_not_an_ai Apr 15 '16 at 13:10
  • Yes unfortunately, my maxsockets is set to 2, and my playerNum is either 0 or 1 (also tested with 1 and 2). – Antsuw Apr 15 '16 at 13:20
  • 1
    What do your constructors, assignment operators, and destructor look like? – AndyG Apr 15 '16 at 13:24
  • `ServerSocket::ServerSocket(int port, int maxsockets){..}` Within this I have : `std::cout << "Server socket created successfully. " << std::endl; clientArray = new TCPsocket[maxsockets]; aSocketSet = SDLNet_AllocSocketSet(maxsockets);` `while (playersConnected < 2) { waitingForConnection(); }` My waitingForConnection() contains the following: `clientArray[slotnum] = newsocket; int n = SDLNet_TCP_AddSocket(aSocketSet, newsocket);` Edit: Sorry for horrible formatting, I am new to this editor. – Antsuw Apr 15 '16 at 13:41
  • 1
    Please edit your question with the code instead of posting it as a comment. – AndyG Apr 15 '16 at 14:03
  • 1
    And did you define a destructor, copy constructor, copy assignment operator? Also, what is happening in the interim before "later on...". Any number of things could have happened to your data in the meantime via copying, etc. Try to reproduce your issue with a very small example and you may even find the problem yourself :-) – AndyG Apr 15 '16 at 14:04
  • 1
    I think getting and using a debugger is the best answer to the last question. It should be able to break on the segfault, then that will tell you the actual cause, and go from there. –  Apr 15 '16 at 14:10
  • I have edited my original question to provide more insight into what's going on. I don't manipulate what is within the array at all after putting the TCPSockets in it.. – Antsuw Apr 15 '16 at 14:26
  • did you check slotnum when you assign a socket to an element? clientArray[slotnum] = newsocket; – HughB Apr 15 '16 at 16:45
  • Print out the value of `playerTurn-1` before you call `serverSocket->waitForPlayer((playerTurn-1));`. Your `changeTurn` function could be misbehaving. We may not have enough information here in your post, so consider providing a [mcve] – AndyG Apr 18 '16 at 21:46

0 Answers0