Bug 106967

Summary: cairo_ft_font_face_create_for_ft_face causes segmentation fault
Product: cairo Reporter: longfeiqiu2012
Component: freetype font backendAssignee: David Turner <david>
Status: RESOLVED MOVED QA Contact: cairo-bugs mailing list <cairo-bugs>
Severity: normal    
Priority: medium    
Version: 1.12.14   
Hardware: x86-64 (AMD64)   
OS: Windows (All)   
Whiteboard:
i915 platform: i915 features:

Description longfeiqiu2012 2018-06-19 19:23:54 UTC
I am using cairo on MinGW-w64, Windows 10. The following code produces a crash:

#include <ft2build.h>
#include <cairo/cairo.h>
#include <cairo/cairo-ft.h>

int main(){
  FT_Library ft;
  FT_Face textFont;
  cairo_font_face_t *cairoFont;
  FT_Init_FreeType(&ft);
  FT_New_Face(ft,"Hack-Regular.ttf",0,&textFont);
  cairoFont = cairo_ft_font_face_create_for_ft_face(textFont,0); // This line produces segmentation fault
  return 0;
}

The problem seems to be that EnterCriticalSection is called before InitializeCriticalSection. A quick workaround is to initialize it manually:

#include <ft2build.h>
#include <cairo/cairo.h>
#include <cairo/cairo-ft.h>

extern "C" void _cairo_mutex_initialize();

int main(){
  FT_Library ft;
  FT_Face textFont;
  cairo_font_face_t *cairoFont;
  FT_Init_FreeType(&ft);
  FT_New_Face(ft,"Hack-Regular.ttf",0,&textFont);
  _cairo_mutex_initialize(); //This eliminates the segfault
  cairoFont = cairo_ft_font_face_create_for_ft_face(textFont,0);
  return 0;
}
Comment 1 Uli Schlachter 2018-06-20 17:20:30 UTC
From a quick look at the code:

Adding CAIRO_MUTEX_INITIALIZE(); to _cairo_ft_unscaled_font_map_lock() should do the trick, but I'm not sure this is the right place to do this. I picked this place since

(a) this is where the mutex is actually locked for the first time
(b) cairo_ft_font_face_create_for_ft_face() pretty much directly calls this function before doing anything (well, through some indirection)

(Sorry, I neither have time to test the above nor can I commit it currently)
Comment 2 longfeiqiu2012 2018-06-28 12:50:26 UTC
This bugs occurs only when cairo is linked statically. Apparently this is because _cairo_mutex_initialize() is called during DllMain, but not when cairo is statically linked. There should be some manual way to initialize cairo.

Also I found this related bug:
https://lists.cairographics.org/archives/cairo/2017-June/028131.html
Comment 3 GitLab Migration User 2018-08-25 14:01:19 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/cairo/cairo/issues/312.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.