SDL Segfault Crash since using SDL_net

This topic is 3266 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all, I've got a very bizarre problem. I've been using SDL for input and graphics in my game for a little while with no problems. Recently I've just started using SDL_net for networking and ever since then the program crashes in a variety of 3rd party libraries (SDL itself, ftgl etc) with a segfault, usually on exiting the program, but occasionally while it is running. Does anyone have any tips for debugging this sort of thing? Whenever I try to see where it crashed it ends up crashing within some other library and not in my code, but it must be something that I have done as it worked before the networking. The networking appears to work as information is sent from one place to another without problems. Below is some code that receives information on the server in case there is something obviously wrong:
UDPpacket* p;
int clientid;

//Allocate memory for packets
p = SDLNet_AllocPacket(PACKET_SIZE);

//Grab all new packets and build them into messages
while(SDLNet_UDP_Recv(this->serversocket, p) == 1)
{
//If we are here then we have received a packet

if(clientid == -1)
{
//We have a new client, create space for him
}
if(clientid == -1)
{
//Send a message back rejecting the poor person
g_cLog.Write("CGameServer::Update() - No space for new clients to connect");
continue;
}

CNetworkPacket*	np;
np = new CNetworkPacket();
memcpy(np, p->data, sizeof(CNetworkPacket) - sizeof(char*));
np->data = new char[np->partdatasize];
memcpy(np->data, &p->data[sizeof(CNetworkPacket) - sizeof(char*)], np->partdatasize);
np->clientid = (char)clientid;

this->clientlist[clientid].temppacketlist.push(np);
}

//Free up packet memory
SDLNet_FreePacket(p);
p = NULL;

//Go through all clients and see if we have any complete messages for processing
this->BuildPackets(elapsed);


Thanks in advance for any help. - James

Share on other sites
Your use of memcpy is suspicious. Can we see your NetworkPacket type?

Share on other sites
Thanks for the feedback, I've just worked out what the problem was. Finally one of the crashes led me to my packet sending function. I checked all my calculations of packet size (when I split large data sets into a number of smaller packets, to be reassembled on the other side) and they turned out to be calculating it incorrectly. I've fixed the sizing calculations and now it works.

My guess for why I was getting all the crazy output is that the packets are created using SDL memory allocation functions (which is probably allocated in the DLL's space?), so when I wrote data outside the memory that was allocated it messed up something else.

Thanks for the help.