Skip to content

Commit d1d73b9

Browse files
committed
free PangoFontMap; fixes #1999
1 parent fff7dfc commit d1d73b9

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/training/pango_font_info.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,20 @@ void PangoFontInfo::SoftInitFontConfig() {
120120
PangoFontMap* PangoFontInfo::get_font_map() {
121121
PangoFontMap* font_map =
122122
pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT);
123-
if (!font_map) // cairo withtout support of Freetype?
123+
if (!font_map) // cairo without support of Freetype?
124124
font_map = pango_cairo_font_map_get_default();
125125
return font_map;
126126
}
127127

128+
// PangoFontMap created with pango_cairo_font_map_new_for_font_type should() be freed
129+
// PangoFontMap created with pango_cairo_font_map_get_default() must not be freed
130+
void PangoFontInfo::free_font_map(PangoFontMap* font_map) {
131+
if (pango_cairo_font_map_get_font_type((PangoCairoFontMap*)(font_map)) == CAIRO_FONT_TYPE_FT) {
132+
g_object_unref(font_map);
133+
font_map = nullptr;
134+
}
135+
}
136+
128137
// Re-initializes font config, whether or not already initialized.
129138
// If already initialized, any existing cache is deleted, just to be sure.
130139
/* static */
@@ -174,6 +183,7 @@ static void ListFontFamilies(PangoFontFamily*** families,
174183
PangoFontMap* font_map = PangoFontInfo::get_font_map();
175184
DISABLE_HEAP_LEAK_CHECK;
176185
pango_font_map_list_families(font_map, families, n_families);
186+
PangoFontInfo::free_font_map(font_map);
177187
}
178188

179189
bool PangoFontInfo::ParseFontDescription(const PangoFontDescription *desc) {
@@ -220,6 +230,7 @@ PangoFont* PangoFontInfo::ToPangoFont() const {
220230
font = pango_font_map_load_font(font_map, context, desc_);
221231
}
222232
g_object_unref(context);
233+
PangoFontInfo::free_font_map(font_map);
223234
return font;
224235
}
225236

@@ -236,6 +247,8 @@ bool PangoFontInfo::CoversUTF8Text(const char* utf8_text, int byte_length) const
236247
int len = it.get_utf8(tmp);
237248
tmp[len] = '\0';
238249
tlog(2, "'%s' (U+%x) not covered by font\n", tmp, *it);
250+
pango_coverage_unref(coverage);
251+
g_object_unref(font);
239252
return false;
240253
}
241254
}
@@ -329,6 +342,7 @@ bool PangoFontInfo::GetSpacingProperties(const std::string& utf8_char,
329342
if (!glyph_index) {
330343
// Glyph for given unicode character doesn't exist in font.
331344
g_object_unref(font);
345+
font = nullptr;
332346
return false;
333347
}
334348
// Find the ink glyph extents for the glyph
@@ -346,6 +360,7 @@ bool PangoFontInfo::GetSpacingProperties(const std::string& utf8_char,
346360
*x_bearing = min_bearing;
347361
*x_advance = total_advance;
348362
g_object_unref(font);
363+
font = nullptr;
349364
return true;
350365
}
351366

@@ -465,6 +480,7 @@ bool PangoFontInfo::CanRenderString(const char* utf8_word, int len,
465480
pango_layout_iter_free(run_iter);
466481
g_object_unref(context);
467482
g_object_unref(layout);
483+
free_font_map(font_map);
468484
if (bad_glyph && graphemes) graphemes->clear();
469485
return !bad_glyph;
470486
}
@@ -499,6 +515,7 @@ bool FontUtils::IsAvailableFont(const char* input_query_desc,
499515
selected_font = pango_font_map_load_font(font_map, context, desc);
500516
}
501517
g_object_unref(context);
518+
PangoFontInfo::free_font_map(font_map);
502519
}
503520
if (selected_font == nullptr) {
504521
pango_font_description_free(desc);

src/training/pango_font_info.h

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class PangoFontInfo {
9595
// FLAGS_fonts_dir and the cache to FLAGS_fontconfig_tmpdir.
9696
static void SoftInitFontConfig();
9797
static PangoFontMap* get_font_map();
98+
static void free_font_map(PangoFontMap* font_map);
9899
// Re-initializes font config, whether or not already initialized.
99100
// If already initialized, any existing cache is deleted, just to be sure.
100101
static void HardInitFontConfig(const std::string& fonts_dir,

0 commit comments

Comments
 (0)