Skip to content

4.2.0+ font rendering mem leak #2629

Closed
Closed

Description

@gompa

What did you do?

use font in loop and draw image

What did you expect to happen?

not leak memory

What actually happened?

leak memory

What versions of Pillow and Python are you using?

it doesnt happen in py3.4 pillow <= 4.1.1
it does in >= 4.20
ImageFont.py leaks memory, it happens on default font, but even more on external loaded font with truetype

when running the script below on version 4.2.0+
mem usage keeps increasing every cycle for :
/usr/local/lib/python3.4/dist-packages/PIL/ImageFont.py:113
/usr/local/lib/python3.4/dist-packages/PIL/ImageFont.py:113: size=15.9 KiB (+15.9 KiB), count=709 (+708), average=23 B
/usr/local/lib/python3.4/dist-packages/PIL/ImageFont.py:113: size=17.2 KiB (+17.2 KiB), count=768 (+767), average=23 B
/usr/local/lib/python3.4/dist-packages/PIL/ImageFont.py:113: size=18.6 KiB (+18.6 KiB), count=827 (+826), average=23 B

from PIL import Image, ImageFont, ImageDraw, ImageFont
import tracemalloc
tracemalloc.start()
count = 0
size = 128, 128
#font2 = ImageFont.truetype('/home/pi/run/fonts/04B_03__.TTF',8) ##use this with own font for more leaking
txt = Image.new('RGBA', size, (255,255,255,0))
d = ImageDraw.Draw(txt)
font2 = ImageFont.load_default()
while True :

	d.text((0, 0), "blabla", font=font2, fill="white")
	if count <= 0:
		snapshot1 = tracemalloc.take_snapshot()
	count += 1
	

	if count == 60 :
		snapshot2 = tracemalloc.take_snapshot()
		count = 1
		top_stats = snapshot2.compare_to(snapshot1, 'lineno')
		print("[ Top 10 differences ]")
		for stat in top_stats[:10]:
			print(stat)

Activity

  1. wiredfool commented on Jul 17, 2017

    @wiredfool
    Member

    Do you have raqm support in 4.2.0?

    from PIL import features
    print (features.check('raqm'))

    This shouldn't affect the default font, but it may point to something.

  2. gompa commented on Jul 17, 2017

    @gompa
    Author

    nope , it returns false
    ( iam using a raspberry pi zero with latest raspbian and python3 , with pillow installed with pip3)

  3. wiredfool commented on Jul 17, 2017

    @wiredfool
    Member

    I've got it leaking in Py3.5, in 4.1.0 and 4.2.1, though the location in 4.1.0 seems off. Similar leak sizes, but not the same. OTOH, I'm not on ARM, so that's not unexpected.

  4. gompa commented on Jul 17, 2017

    @gompa
    Author

    on 4.1.1 py3.4 i can get it to leak in /usr/local/lib/python3.4/dist-packages/PIL/ImageDraw.py:222

    moving the following into the loop does not seem to help either (i thought maybe it was because of the object not being removed

    	d = ImageDraw.Draw(txt)
    	d.text((0, 0), "blabla", font=font2, fill="white")
    	del d
  5. wiredfool commented on Jul 17, 2017

    @wiredfool
    Member

    I think what's going on here is in _imaging.c, around line 2170

    void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
        PyObject* bytes = NULL;
    
        *text = NULL;
    
        if (PyUnicode_CheckExact(encoded_string)){
            bytes = PyUnicode_AsLatin1String(encoded_string);
        } else if (PyBytes_Check(encoded_string)) {
            bytes = encoded_string;
        }
        if (bytes) {
            *text = (unsigned char*)PyBytes_AsString(bytes);
            return;
        }

    PyUnicode_AsLatin1String returns a new reference, which is then used by PyBytes_AsString to give us a char* pointer. But it's never freed, it's just dropped. It can't be freed here, because it needs to live as long as the text pointer is alive.

    This code is likely the root of #2620 as well, as it's common to both code paths and it's the only place latin1 is mentioned.

  6. wiredfool commented on Jul 18, 2017

    @wiredfool
    Member

    And similarly, in text_layout_*,

        (*glyph_info) = PyMem_New(GlyphInfo, count);
        if ((*glyph_info) == NULL) {
            PyErr_SetString(PyExc_MemoryError, "PyMem_New() failed");
            return 0;
        }

    glyph_info is returned, but never freed by the caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions