- Fix memory leaks in the server start menu by not

creating the maplist each time it's called, but
  preserve it across several calls.

- Fix M_PushMenu a second time by taking a corner
  case into account when the requested menu is 
  opened and on the stack but not on top.
This commit is contained in:
Daniel Gibson 2012-02-26 20:21:40 +00:00
parent 32377a6b57
commit b70dc11672

View file

@ -108,8 +108,27 @@ void M_PopMenu (void) {
M_ForceMenuOff ();
}
/*
* This crappy function maintaines a stack of opened menus.
* The steps in this horrible mess are:
*
* 1. But the game into pause if a menu is opened
*
* 2. If the requested menu is already open, close it.
*
* 3. If the requested menu is already open but not
* on top, close all menus above it and the menu
* itself. This is necessary since an instance of
* the reqeuested menu is in flight and will be
* displayed.
*
* 4. Save the previous menu on top (which was in flight)
* to the stack and make the requested menu the menu in
* flight.
*/
static void M_PushMenu ( void (*draw) (void), const char *(*key) (int) ) {
int i;
int alreadyPresent = 0;
if (Cvar_VariableValue ("maxclients") == 1
&& Com_ServerState ())
@ -124,16 +143,16 @@ static void M_PushMenu ( void (*draw) (void), const char *(*key) (int) ) {
/* if this menu is already present, drop back to that level
to avoid stacking menus by hotkeys */
for (i=0 ; i<m_menudepth ; i++)
{
if (m_layers[i].draw == draw &&
m_layers[i].key == key) {
alreadyPresent = 1;
break;
}
}
while(i < m_menudepth) { // menu was already opened further down the stack
// pop until we are at the point where this menu was opened the last time
while(alreadyPresent && i <= m_menudepth) { // menu was already opened further down the stack
// pop everything above the old instance of this menu and that instance itself
M_PopMenu(); // decrements m_menudepth
}
@ -2296,8 +2315,8 @@ static void M_Menu_JoinServer_f (void) {
* =================
*/
static menuframework_s s_startserver_menu;
static char **mapnames;
static int nummaps;
static char **mapnames = NULL;
static int nummaps = 0;
static menuaction_s s_startserver_start_action;
static menuaction_s s_startserver_dmoptions_action;
@ -2415,6 +2434,7 @@ static void StartServer_MenuInit( void ) {
int i;
FILE *fp;
if(mapnames == NULL) { // initialize list of maps once, reuse it afterwards (=> it isn't freed)
/* load the list of map names */
Com_sprintf( mapsname, sizeof( mapsname ), "%s/maps.lst", FS_Gamedir() );
@ -2471,12 +2491,14 @@ static void StartServer_MenuInit( void ) {
mapnames[nummaps] = 0;
if ( fp != 0 ) {
fclose(fp);
fp = 0;
free( buffer );
} else {
FS_FreeFile( buffer );
}
}
/* initialize the menu stuff */
s_startserver_menu.x = (int)(viddef.width * 0.50f);
@ -2592,20 +2614,6 @@ static void StartServer_MenuDraw(void) {
}
static const char *StartServer_MenuKey( int key ) {
if ( key == K_ESCAPE ) {
if ( mapnames ) {
int i;
for ( i = 0; i < nummaps; i++ )
free( mapnames[i] );
free( mapnames );
}
mapnames = 0;
nummaps = 0;
}
return Default_MenuKey( &s_startserver_menu, key );
}