From a63482883e0e3487da4544699fa9c419cb17127d Mon Sep 17 00:00:00 2001 From: Thilo Schulz Date: Fri, 15 Jul 2011 14:44:06 +0000 Subject: - Revert back to Z_Malloc from Hunk_FreeTempMemory introduced in r2077 as Hunk_FreeTempMemory must be freed in LIFO order (#5079) - Introduce SV_ClientFree() to prevent memory leaks r2077 was supposed to fix --- src/server/server.h | 3 ++- src/server/sv_client.c | 41 +++++++++++++++++++++++++++++------------ src/server/sv_init.c | 10 ++++++++-- src/server/sv_net_chan.c | 25 +++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/src/server/server.h b/src/server/server.h index 7887a2c3..9fcce725 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -331,6 +331,7 @@ void SV_ExecuteClientMessage( client_t *cl, msg_t *msg ); void SV_UserinfoChanged( client_t *cl ); void SV_ClientEnterWorld( client_t *client, usercmd_t *cmd ); +void SV_FreeClient(client_t *client); void SV_DropClient( client_t *drop, const char *reason ); void SV_ExecuteClientCommand( client_t *cl, const char *s, qboolean clientOK ); @@ -431,4 +432,4 @@ void SV_ClipToEntity( trace_t *trace, const vec3_t start, const vec3_t mins, con void SV_Netchan_Transmit( client_t *client, msg_t *msg); int SV_Netchan_TransmitNextFragment(client_t *client); qboolean SV_Netchan_Process( client_t *client, msg_t *msg ); - +void SV_Netchan_FreeQueue(client_t *client); diff --git a/src/server/sv_client.c b/src/server/sv_client.c index b9b5e659..080349f8 100644 --- a/src/server/sv_client.c +++ b/src/server/sv_client.c @@ -370,6 +370,29 @@ gotnewcl: } } +/* +===================== +SV_FreeClient + +Destructor for data allocated in a client structure +===================== +*/ +void SV_FreeClient(client_t *client) +{ + int index; + + SV_Netchan_FreeQueue(client); + SV_CloseDownload(client); + + for(index = client->queuedVoipIndex; index < client->queuedVoipPackets; index++) + { + index %= ARRAY_LEN(client->voipPacket); + + Z_Free(client->voipPacket[index]); + } + + client->queuedVoipPackets = 0; +} /* ===================== @@ -398,17 +421,12 @@ void SV_DropClient( client_t *drop, const char *reason ) { } } - // Kill any download - SV_CloseDownload( drop ); + // Free all allocated data on the client structure + SV_FreeClient(drop); // tell everyone why they got dropped SV_SendServerCommand( NULL, "print \"%s" S_COLOR_WHITE " %s\n\"", drop->name, reason ); - if (drop->download) { - FS_FCloseFile( drop->download ); - drop->download = 0; - } - // call the prog function for removing a client // this will remove the body, among other things VM_Call( gvm, GAME_CLIENT_DISCONNECT, drop - svs.clients ); @@ -574,7 +592,7 @@ static void SV_CloseDownload( client_t *cl ) { // Free the temporary buffer space for (i = 0; i < MAX_DOWNLOAD_WINDOW; i++) { if (cl->downloadBlocks[i]) { - Hunk_FreeTempMemory(cl->downloadBlocks[i]); + Z_Free(cl->downloadBlocks[i]); cl->downloadBlocks[i] = NULL; } } @@ -766,7 +784,7 @@ int SV_WriteDownloadToClient(client_t *cl, msg_t *msg) curindex = (cl->downloadCurrentBlock % MAX_DOWNLOAD_WINDOW); if (!cl->downloadBlocks[curindex]) - cl->downloadBlocks[curindex] = Hunk_AllocateTempMemory(MAX_DOWNLOAD_BLKSIZE); + cl->downloadBlocks[curindex] = Z_Malloc(MAX_DOWNLOAD_BLKSIZE); cl->downloadBlockSize[curindex] = FS_Read( cl->downloadBlocks[curindex], MAX_DOWNLOAD_BLKSIZE, cl->download ); @@ -944,7 +962,7 @@ void SV_WriteVoipToClient( client_t *cl, msg_t *msg ) MSG_WriteShort(msg, packet->len); MSG_WriteData(msg, packet->data, packet->len); - Hunk_FreeTempMemory(packet); + Z_Free(packet); } cl->queuedVoipPackets -= i; @@ -1626,13 +1644,12 @@ void SV_UserVoip( client_t *cl, msg_t *msg ) { continue; // not addressed to this player. // Transmit this packet to the client. - // !!! FIXME: I don't like this queueing system. if (client->queuedVoipPackets >= ARRAY_LEN(client->voipPacket)) { Com_Printf("Too many VoIP packets queued for client #%d\n", i); continue; // no room for another packet right now. } - packet = Hunk_AllocateTempMemory(sizeof(*packet)); + packet = Z_Malloc(sizeof(*packet)); packet->sender = sender; packet->frames = frames; packet->len = packetsize; diff --git a/src/server/sv_init.c b/src/server/sv_init.c index d2e51d9f..4c0ae128 100644 --- a/src/server/sv_init.c +++ b/src/server/sv_init.c @@ -737,8 +737,14 @@ void SV_Shutdown( char *finalmsg ) { SV_ClearServer(); // free server static data - if ( svs.clients ) { - Z_Free( svs.clients ); + if(svs.clients) + { + int index; + + for(index = 0; index < sv_maxclients->integer; index++) + SV_FreeClient(&svs.clients[index]); + + Z_Free(svs.clients); } Com_Memset( &svs, 0, sizeof( svs ) ); diff --git a/src/server/sv_net_chan.c b/src/server/sv_net_chan.c index 79e294f0..a4304566 100644 --- a/src/server/sv_net_chan.c +++ b/src/server/sv_net_chan.c @@ -131,6 +131,27 @@ static void SV_Netchan_Decode( client_t *client, msg_t *msg ) { } #endif + + +/* +================= +SV_Netchan_FreeQueue +================= +*/ +void SV_Netchan_FreeQueue(client_t *client) +{ + netchan_buffer_t *netbuf, *next; + + for(netbuf = client->netchan_start_queue; netbuf; netbuf = next) + { + next = netbuf->next; + Z_Free(netbuf); + } + + client->netchan_start_queue = NULL; + client->netchan_end_queue = &client->netchan_start_queue; +} + /* ================= SV_Netchan_TransmitNextInQueue @@ -160,7 +181,7 @@ void SV_Netchan_TransmitNextInQueue(client_t *client) else Com_DPrintf("#462 Netchan_TransmitNextFragment: remaining queued message\n"); - Hunk_FreeTempMemory(netbuf); + Z_Free(netbuf); } /* @@ -208,7 +229,7 @@ void SV_Netchan_Transmit( client_t *client, msg_t *msg) { netchan_buffer_t *netbuf; Com_DPrintf("#462 SV_Netchan_Transmit: unsent fragments, stacked\n"); - netbuf = (netchan_buffer_t *) Hunk_AllocateTempMemory(sizeof(netchan_buffer_t)); + netbuf = (netchan_buffer_t *) Z_Malloc(sizeof(netchan_buffer_t)); // store the msg, we can't store it encoded, as the encoding depends on stuff we still have to finish sending MSG_Copy(&netbuf->msg, netbuf->msgBuffer, sizeof( netbuf->msgBuffer ), msg); netbuf->next = NULL; -- cgit