[client] main: destroy and finish fontconfig usage

Fixes ASAN reported memory leak
This commit is contained in:
Geoffrey McRae 2021-08-08 16:21:48 +10:00
parent 30ed563504
commit f4a925a750
3 changed files with 35 additions and 11 deletions

View File

@ -49,6 +49,8 @@ static inline double util_clamp(double x, double min, double max)
return x;
}
bool util_initUIFonts(void);
void util_freeUIFonts(void);
char * util_getUIFont(const char * fontName);
#endif

View File

@ -827,8 +827,11 @@ static int lg_run(void)
g_state.io->BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
g_state.windowScale = 1.0;
if (util_initUIFonts())
{
g_state.fontName = util_getUIFont(g_params.uiFont);
DEBUG_INFO("Using font: %s", g_state.fontName);
}
app_initOverlays();
@ -1306,6 +1309,7 @@ int main(int argc, char * argv[])
config_free();
util_freeUIFonts();
cleanupCrashHandler();
return ret;
}

View File

@ -31,6 +31,8 @@
#include <math.h>
#include <fontconfig/fontconfig.h>
static FcConfig * FontConfig = NULL;
bool util_fileGetContents(const char * filename, char ** buffer, size_t * length)
{
FILE * fh = fopen(filename, "r");
@ -259,20 +261,26 @@ int util_mergeOverlappingRects(FrameDamageRect * rects, int count)
return o;
}
char * util_getUIFont(const char * fontName)
bool util_initUIFonts(void)
{
static FcConfig * fc = NULL;
char * ttf = NULL;
if (FontConfig)
return true;
if (!fc)
fc = FcInitLoadConfigAndFonts();
FontConfig = FcInitLoadConfigAndFonts();
if (!fc)
if (!FontConfig)
{
DEBUG_ERROR("FcInitLoadConfigAndFonts Failed");
return NULL;
return false;
}
return true;
}
char * util_getUIFont(const char * fontName)
{
char * ttf = NULL;
FcPattern * pat = FcNameParse((const FcChar8*) fontName);
if (!pat)
{
@ -280,11 +288,11 @@ char * util_getUIFont(const char * fontName)
return NULL;
}
FcConfigSubstitute(fc, pat, FcMatchPattern);
FcConfigSubstitute(FontConfig, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
FcResult result;
FcChar8 * file = NULL;
FcPattern * match = FcFontMatch(fc, pat, &result);
FcPattern * match = FcFontMatch(FontConfig, pat, &result);
if (!match)
{
@ -303,3 +311,13 @@ fail_parse:
FcPatternDestroy(pat);
return ttf;
}
void util_freeUIFonts(void)
{
if (!FontConfig)
return;
FcConfigDestroy(FontConfig);
FontConfig = NULL;
FcFini();
}