[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; return x;
} }
bool util_initUIFonts(void);
void util_freeUIFonts(void);
char * util_getUIFont(const char * fontName); char * util_getUIFont(const char * fontName);
#endif #endif

View File

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

View File

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