Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b17687

Browse files
committed
Fix leaks of FT2Font objects.
svn path=/trunk/matplotlib/; revision=3750
1 parent feab121 commit 5b17687

3 files changed

Lines changed: 31 additions & 18 deletions

File tree

lib/matplotlib/backends/backend_agg.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@
8989
from _backend_agg import RendererAgg as _RendererAgg
9090

9191
backend_version = 'v2.2'
92-
_fontd = {} # a map from fname to font instances
93-
9492

9593
class RendererAgg(RendererBase):
9694
"""
@@ -126,7 +124,8 @@ def __init__(self, width, height, dpi):
126124
self.copy_from_bbox = self._renderer.copy_from_bbox
127125
self.restore_region = self._renderer.restore_region
128126
self.mathtext_parser = MathTextParser('Agg')
129-
127+
self._fontd = {}
128+
130129
self.bbox = lbwh_to_bbox(0,0, self.width, self.height)
131130
if __debug__: verbose.report('RendererAgg.__init__ done',
132131
'debug-annoying')
@@ -298,12 +297,12 @@ def _get_agg_font(self, prop):
298297
'debug-annoying')
299298

300299
key = hash(prop)
301-
font = _fontd.get(key)
300+
font = self._fontd.get(key)
302301

303302
if font is None:
304303
fname = findfont(prop)
305304
font = FT2Font(str(fname))
306-
_fontd[key] = font
305+
self._fontd[key] = font
307306

308307
font.clear()
309308
size = prop.get_size_in_points()

lib/matplotlib/backends/backend_svg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def new_figure_manager(num, *args, **kwargs):
2323
return manager
2424

2525

26-
_fontd = {}
2726
_capstyle_d = {'projecting' : 'square', 'butt' : 'butt', 'round': 'round',}
2827
class RendererSVG(RendererBase):
2928
FONT_SCALE = 1200.0
@@ -41,6 +40,7 @@ def __init__(self, width, height, svgwriter, basename=None):
4140
self._clipd = {}
4241
self._char_defs = {}
4342
self.mathtext_parser = MathTextParser('SVG')
43+
self.fontd = {}
4444
svgwriter.write(svgProlog%(width,height,width,height))
4545

4646
def _draw_svg_element(self, element, details, gc, rgbFace):
@@ -56,11 +56,11 @@ def _draw_svg_element(self, element, details, gc, rgbFace):
5656

5757
def _get_font(self, prop):
5858
key = hash(prop)
59-
font = _fontd.get(key)
59+
font = self.fontd.get(key)
6060
if font is None:
6161
fname = findfont(prop)
6262
font = FT2Font(str(fname))
63-
_fontd[key] = font
63+
self.fontd[key] = font
6464
font.clear()
6565
size = prop.get_size_in_points()
6666
font.set_size(size, 72.0)

lib/matplotlib/mathtext.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ def __init__(self, default_font_prop, mathtext_backend):
430430
self.mathtext_backend.fonts_object = self
431431
self.used_characters = {}
432432

433+
def destroy(self):
434+
"""Fix any cyclical references before the object is about
435+
to be destroyed."""
436+
self.used_characters = None
437+
433438
def get_kern(self, font1, sym1, fontsize1,
434439
font2, sym2, fontsize2, dpi):
435440
"""
@@ -461,7 +466,7 @@ def get_metrics(self, font, sym, fontsize, dpi):
461466
xmin, xmax, ymin, ymax - the ink rectangle of the glyph
462467
iceberg - the distance from the baseline to the top of the glyph.
463468
horiBearingY in Truetype parlance, height in TeX parlance
464-
"""
469+
"""
465470
info = self._get_info(font, sym, fontsize, dpi)
466471
return info.metrics
467472

@@ -494,19 +499,17 @@ def get_results(self, box):
494499
return self.mathtext_backend.get_results(box)
495500

496501
def get_sized_alternatives_for_symbol(self, fontname, sym):
497-
"""Override if your font provides multiple sizes of the same
498-
symbol."""
502+
"""
503+
Override if your font provides multiple sizes of the same
504+
symbol.
505+
"""
499506
return [(fontname, sym)]
500507

501508
class TruetypeFonts(Fonts):
502509
"""
503510
A generic base class for all font setups that use Truetype fonts
504511
(through ft2font)
505512
"""
506-
"""
507-
Use the Bakoma true type fonts for rendering
508-
"""
509-
# allocate a new set of fonts
510513
basepath = os.path.join( get_data_path(), 'fonts', 'ttf' )
511514

512515
class CachedFont:
@@ -529,6 +532,14 @@ def __init__(self, default_font_prop, mathtext_backend):
529532

530533
self.fonts['default'] = default_font
531534

535+
def destroy(self):
536+
self.glyphd = None
537+
for cached_font in self.fonts.values():
538+
cached_font.charmap = None
539+
cached_font.glyphmap = None
540+
cached_font.font = None
541+
Fonts.destroy(self)
542+
532543
def _get_font(self, font):
533544
"""Looks up a CachedFont with its charmap and inverse charmap.
534545
font may be a TeX font name (cal, rm, it etc.), or postscript name."""
@@ -2401,7 +2412,7 @@ def auto_sized_delimiter(self, s, loc, toks):
24012412
##############################################################################
24022413
# MAIN
24032414

2404-
class MathTextParser:
2415+
class MathTextParser(object):
24052416
"""
24062417
Parse the math expression s, return the (bbox, fonts) tuple needed
24072418
to render it.
@@ -2453,7 +2464,10 @@ def parse(self, s, dpi, prop):
24532464
self._cache[cacheKey] = result
24542465
# Free up the transient data structures
24552466
self._parser.clear()
2456-
# Remove a cyclical reference
2457-
font_output.mathtext_backend.fonts_object = None
24582467

2468+
# Fix cyclical references
2469+
font_output.destroy()
2470+
font_output.mathtext_backend.fonts_object = None
2471+
font_output.mathtext_backend = None
2472+
24592473
return result

0 commit comments

Comments
 (0)