(svn r10082) -Fix [FS#846]: another memory leak in the networking code (benc).
authorrubidium
Sun, 10 Jun 2007 19:59:08 +0000
changeset 7339 661958642f62
parent 7338 5bb335d2eb54
child 7340 0ac181725a79
(svn r10082) -Fix [FS#846]: another memory leak in the networking code (benc).
src/network/network_data.cpp
--- a/src/network/network_data.cpp	Sun Jun 10 10:52:19 2007 +0000
+++ b/src/network/network_data.cpp	Sun Jun 10 19:59:08 2007 +0000
@@ -30,65 +30,61 @@
 // Prepare a DoCommand to be send over the network
 void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
 {
-	CommandPacket *c = MallocT<CommandPacket>(1);
-	byte temp_callback;
+	CommandPacket c;
 
-	c->player = _local_player;
-	c->next = NULL;
-	c->tile = tile;
-	c->p1 = p1;
-	c->p2 = p2;
-	c->cmd = cmd;
-	c->callback = 0;
+	c.player = _local_player;
+	c.next   = NULL;
+	c.tile   = tile;
+	c.p1     = p1;
+	c.p2     = p2;
+	c.cmd    = cmd;
 
-	temp_callback = 0;
+	c.callback = 0;
+	while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) {
+		c.callback++;
+	}
 
-	while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback)
-		temp_callback++;
-	if (temp_callback == _callback_table_count) {
+	if (c.callback == _callback_table_count) {
 		DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
-		temp_callback = 0; /* _callback_table[0] == NULL */
+		c.callback = 0; // _callback_table[0] == NULL
 	}
 
+	ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text));
+
 	if (_network_server) {
-		// We are the server, so set the command to be executed next possible frame
-		c->frame = _frame_counter_max + 1;
-	} else {
-		c->frame = 0; // The client can't tell which frame, so just make it 0
-	}
-
-	ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text));
+		/* If we are the server, we queue the command in our 'special' queue.
+		 *   In theory, we could execute the command right away, but then the
+		 *   client on the server can do everything 1 tick faster than others.
+		 *   So to keep the game fair, we delay the command with 1 tick
+		 *   which gives about the same speed as most clients.
+		 */
+		c.frame = _frame_counter_max + 1;
 
-	if (_network_server) {
-		// If we are the server, we queue the command in our 'special' queue.
-		//   In theory, we could execute the command right away, but then the
-		//   client on the server can do everything 1 tick faster than others.
-		//   So to keep the game fair, we delay the command with 1 tick
-		//   which gives about the same speed as most clients.
-		NetworkTCPSocketHandler *cs;
-
-		// And we queue it for delivery to the clients
-		FOR_ALL_CLIENTS(cs) {
-			if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c);
+		CommandPacket *new_cp = MallocT<CommandPacket>(1);
+		*new_cp = c;
+		if (_local_command_queue == NULL) {
+			_local_command_queue = new_cp;
+		} else {
+			/* Find last packet */
+			CommandPacket *cp = _local_command_queue;
+			while (cp->next != NULL) cp = cp->next;
+			cp->next = new_cp;
 		}
 
-		// Only the server gets the callback, because clients should not get them
-		c->callback = temp_callback;
-		if (_local_command_queue == NULL) {
-			_local_command_queue = c;
-		} else {
-			// Find last packet
-			CommandPacket *cp = _local_command_queue;
-			while (cp->next != NULL) cp = cp->next;
-			cp->next = c;
+		/* Only the local client (in this case, the server) gets the callback */
+		c.callback = 0;
+		/* And we queue it for delivery to the clients */
+		NetworkTCPSocketHandler *cs;
+		FOR_ALL_CLIENTS(cs) {
+			if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, &c);
 		}
-
 		return;
 	}
 
-	// Clients send their command to the server and forget all about the packet
-	c->callback = temp_callback;
-	SEND_COMMAND(PACKET_CLIENT_COMMAND)(c);
+	c.frame = 0; // The client can't tell which frame, so just make it 0
+
+	/* Clients send their command to the server and forget all about the packet */
+	SEND_COMMAND(PACKET_CLIENT_COMMAND)(&c);
 }
 
 // Execute a DoCommand we received from the network