{"action":"created","issue":{"url":"https://api.github.com/repos/gtkforphp/cairo/issues/17","labels_url":"https://api.github.com/repos/gtkforphp/cairo/issues/17/labels{/name}","comments_url":"https://api.github.com/repos/gtkforphp/cairo/issues/17/comments","events_url":"https://api.github.com/repos/gtkforphp/cairo/issues/17/events","html_url":"https://github.com/gtkforphp/cairo/pull/17","id":75974429,"number":17,"title":"fixed destruction of FT-fonts and associated resources","user":{"login":"mhertsch","id":8458597,"avatar_url":"https://avatars.githubusercontent.com/u/8458597?v=3","gravatar_id":"","url":"https://api.github.com/users/mhertsch","html_url":"https://github.com/mhertsch","followers_url":"https://api.github.com/users/mhertsch/followers","following_url":"https://api.github.com/users/mhertsch/following{/other_user}","gists_url":"https://api.github.com/users/mhertsch/gists{/gist_id}","starred_url":"https://api.github.com/users/mhertsch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhertsch/subscriptions","organizations_url":"https://api.github.com/users/mhertsch/orgs","repos_url":"https://api.github.com/users/mhertsch/repos","events_url":"https://api.github.com/users/mhertsch/events{/privacy}","received_events_url":"https://api.github.com/users/mhertsch/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-05-13T12:53:34Z","updated_at":"2015-06-26T08:07:41Z","closed_at":"2015-06-24T21:57:14Z","pull_request":{"url":"https://api.github.com/repos/gtkforphp/cairo/pulls/17","html_url":"https://github.com/gtkforphp/cairo/pull/17","diff_url":"https://github.com/gtkforphp/cairo/pull/17.diff","patch_url":"https://github.com/gtkforphp/cairo/pull/17.patch"},"body":"This pull request is in response to issues #2 and 'cairo font cache poisoning' that was introduced with the solution in #15 by @medic123de.\r\n\r\nBackground:\r\n\r\nIn january we had issues based on the last changes in master, that separated the ft_lib. @medic123de produced a patch, that removed the core file issue and all seemed good.\r\n\r\nA couple weeks ago, we discovered, that if one is creating a lot of pictures with text, sometimes, the text is not visible at all or single glyphs are missing. \r\n\r\nAfter haveing a simple script to reproduce the issue, searching the web about simliar symptons when using cairo and ft-fonts, and looking at the code in pecl cairo, I found the current master (and the patch from @medic123de) do not adhere to what should be done about freeing the FT resources in a cairo environemt. http://cairo.cairographics.narkive.com/qINWvRx7/issue-with-cairo-font-cache\r\n\r\nAs long as cairo is holding a reference to the font used in its internal font cache, one can not call FT_Done_Face in its own code. \r\nCalling FT_Done_Face as callback in cairo_font_face_set_user_data when the FT_Lib might already have been destroyed in cairo_ft_font_face_object_destroy caused the core files.\r\n\r\nTo address these issues, I created a patch and introduced a new function cairo_user_data_callback_ft_free, that contains everything that needs to be freed after cairo is done with this specific ft-font. The memory associated needs to be allocated to last longer then the request itself, aka in the process scope, otherwise, the cairo font cache would again hold references to already freed memory.\r\n\r\nI have attached a simple script for reproduction. It is not neccessary to actualy draw text. It is enough to check the fontExtends for invalid values.\r\n\r\nWith the current master, this script should produce core files from time to time (cent os 6 + php 54 from remi repositories). With the patch #15 from @medic123de, after a few requests per php-fpm child, the error counter which is outputed into the error_log will increase.\r\n\r\nWith this patch, there should be neither core files, nor an increase in memory, or text drawn with missing glyphs.\r\n\r\n    <?php\r\n    $surface = new \\CairoImageSurface(CairoFormat::ARGB32, 100, 10);\r\n    $context = new \\CairoContext($surface);\r\n\r\n\r\n    $errorCounter = 0;\r\n    $counter = 0;\r\n\r\n    for ($i = 0; $i < 600; ++$i, ++$counter) {\r\n        $fontFace = new \\CairoFtFontFace(dirname(__FILE__) . \"/arial.ttf\");\r\n        $context->setFontFace($fontFace);\r\n        $fontExtents = $context->fontExtents();\r\n        if ($fontExtents['height'] == 0) {\r\n            $errorCounter++;\r\n        }\r\n    }\r\n\r\n    error_log(getmypid() . ' Number of errors: ' . $errorCounter . '/' . $counter);\r\n\r\n    $surface->flush();\r\n    $surface->finish();\r\n\r\n\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/gtkforphp/cairo/issues/comments/115568061","html_url":"https://github.com/gtkforphp/cairo/pull/17#issuecomment-115568061","issue_url":"https://api.github.com/repos/gtkforphp/cairo/issues/17","id":115568061,"user":{"login":"mhertsch","id":8458597,"avatar_url":"https://avatars.githubusercontent.com/u/8458597?v=3","gravatar_id":"","url":"https://api.github.com/users/mhertsch","html_url":"https://github.com/mhertsch","followers_url":"https://api.github.com/users/mhertsch/followers","following_url":"https://api.github.com/users/mhertsch/following{/other_user}","gists_url":"https://api.github.com/users/mhertsch/gists{/gist_id}","starred_url":"https://api.github.com/users/mhertsch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhertsch/subscriptions","organizations_url":"https://api.github.com/users/mhertsch/orgs","repos_url":"https://api.github.com/users/mhertsch/repos","events_url":"https://api.github.com/users/mhertsch/events{/privacy}","received_events_url":"https://api.github.com/users/mhertsch/received_events","type":"User","site_admin":false},"created_at":"2015-06-26T08:07:40Z","updated_at":"2015-06-26T08:07:40Z","body":"Cool, Thanks :)"}}