Sign in to follow this  

SDL Segfault Crash since using SDL_net

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
	clientid = this->GetClientID(p->, p->address.port);

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

	//Load in the data
	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;


//Free up packet memory
p = NULL;

//Go through all clients and see if we have any complete messages for processing

Thanks in advance for any help. - James

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this