Fix possible memory leak in menu font and crosshair loading.

This commit is contained in:
Knightmare66 2019-10-11 15:30:46 -04:00
parent 3da6641b19
commit 2959cb730a
3 changed files with 19 additions and 9 deletions

View file

@ -194,7 +194,7 @@ typedef struct
// This can be used to load script files, etc // This can be used to load script files, etc
// Also support open, read/write and closing files // Also support open, read/write and closing files
#ifdef KMQUAKE2_ENGINE_MOD #ifdef KMQUAKE2_ENGINE_MOD
char **(*ListPak) (char *find, int *num); char **(*ListPak) (char *find, int *num); // Deprecated- DO NOT USE!
int (*LoadFile) (char *name, void **buf); int (*LoadFile) (char *name, void **buf);
void (*FreeFile) (void *buf); void (*FreeFile) (void *buf);
void (*FreeFileList) (char **list, int n); void (*FreeFileList) (char **list, int n);

View file

@ -101,8 +101,8 @@ Font loading
*/ */
cvar_t *con_font; cvar_t *con_font;
#define MAX_FONTS 32 #define MAX_FONTS 32
char **font_names; char **font_names = NULL;
int numfonts; int numfonts = 0;
static void FontSizeFunc( void *unused ) static void FontSizeFunc( void *unused )
{ {
@ -122,7 +122,7 @@ void SetFontCursor (void)
if (!con_font) if (!con_font)
con_font = Cvar_Get ("con_font", "default", CVAR_ARCHIVE); con_font = Cvar_Get ("con_font", "default", CVAR_ARCHIVE);
if (numfonts>1) if (numfonts > 1)
for (i=0; font_names[i]; i++) for (i=0; font_names[i]; i++)
{ {
if (!Q_strcasecmp(con_font->string, font_names[i])) if (!Q_strcasecmp(con_font->string, font_names[i]))
@ -203,7 +203,7 @@ char **SetFontNames (void)
nfontnames++; nfontnames++;
} }
//set back so whole string get deleted. // set back so whole string get deleted.
p[num] = '.'; p[num] = '.';
} }
if (nfonts) if (nfonts)
@ -234,7 +234,7 @@ char **SetFontNames (void)
if (!FS_ItemInList(curFont, nfontnames, list)) if (!FS_ItemInList(curFont, nfontnames, list))
{ {
insertFont(list, strdup(curFont),nfontnames); insertFont(list, strdup(curFont), nfontnames);
nfontnames++; nfontnames++;
} }
@ -348,6 +348,11 @@ void Options_Interface_MenuInit ( void )
s_options_interface_menualpha_slider.maxvalue = 20; s_options_interface_menualpha_slider.maxvalue = 20;
s_options_interface_menualpha_slider.generic.statusbar = "changes opacity of menu background"; s_options_interface_menualpha_slider.generic.statusbar = "changes opacity of menu background";
// free any loaded fonts to prevent memory leak
if (numfonts > 0) {
FS_FreeFileList (font_names, numfonts);
}
numfonts = 0;
font_names = SetFontNames (); font_names = SetFontNames ();
s_options_interface_font_box.generic.type = MTYPE_SPINCONTROL; s_options_interface_font_box.generic.type = MTYPE_SPINCONTROL;
s_options_interface_font_box.generic.x = 0; s_options_interface_font_box.generic.x = 0;

View file

@ -97,8 +97,8 @@ Crosshair loading
*/ */
#define MAX_CROSSHAIRS 100 #define MAX_CROSSHAIRS 100
char **crosshair_names; char **crosshair_names = NULL;
int numcrosshairs; int numcrosshairs = 0;
/*static void OldCrosshairFunc( void *unused ) /*static void OldCrosshairFunc( void *unused )
{ {
@ -169,7 +169,7 @@ char **SetCrosshairNames (void)
list = malloc( sizeof( char * ) * MAX_CROSSHAIRS+1 ); list = malloc( sizeof( char * ) * MAX_CROSSHAIRS+1 );
memset( list, 0, sizeof( char * ) * MAX_CROSSHAIRS+1 ); memset( list, 0, sizeof( char * ) * MAX_CROSSHAIRS+1 );
list[0] = strdup("none"); //was default list[0] = strdup("none"); // was default
ncrosshairnames = 1; ncrosshairnames = 1;
path = FS_NextPath( path ); path = FS_NextPath( path );
@ -343,6 +343,11 @@ void Options_Screen_MenuInit ( void )
s_options_screen_header.generic.x = MENU_FONT_SIZE/2 * strlen(s_options_screen_header.generic.name); s_options_screen_header.generic.x = MENU_FONT_SIZE/2 * strlen(s_options_screen_header.generic.name);
s_options_screen_header.generic.y = 0; s_options_screen_header.generic.y = 0;
// free any loaded crosshairs to prevent memory leak
if (numcrosshairs > 0) {
FS_FreeFileList (crosshair_names, numcrosshairs);
}
numcrosshairs = 0;
crosshair_names = SetCrosshairNames (); crosshair_names = SetCrosshairNames ();
s_options_screen_crosshair_box.generic.type = MTYPE_SPINCONTROL; s_options_screen_crosshair_box.generic.type = MTYPE_SPINCONTROL;
s_options_screen_crosshair_box.generic.x = 0; s_options_screen_crosshair_box.generic.x = 0;