Subject: bug#18501: Acknowledgement (24.3.93; OS X; crash in free() when calling macfont_close())

From: Dmitry Antipov <[email-masked]>
Date: Sat, 20 Sep 2014 05:08:15 +0400

On 09/19/2014 10:05 PM, Jim Radford wrote:

> Here are the two calls that free the font:
>
>      frame #1: 0x00000001001c5ffd Emacs`macfont_close(font=0x0000000105c2a8c0) + 13 at macfont.m:2621
>      frame #2: 0x000000010014de80 Emacs`font_clear_cache(f=<unavailable>, cache=<unavailable>, driver=<unavailable>) + 304 at font.c:2620
>
>      frame #1: 0x00000001001c5ffd Emacs`macfont_close(font=0x0000000105c2a8c0) + 13 at macfont.m:2621
>      frame #2: 0x000000010011be9d Emacs`Fgarbage_collect [inlined] cleanup_vector + 38 at alloc.c:2935
>
> Notice that the pointer is the same in both cases.  Both cleanup_vector() and font_clear_cache() call
>
>      drv->close(font)
>
> It seems that font_clear_cache is leaving the font around for the GC to clean up (a second time) later.

Please try this.

Dmitry

=== modified file 'src/macfont.m'
--- src/macfont.m	2014-07-20 13:18:47 +0000
+++ src/macfont.m	2014-09-20 01:05:58 +0000
@@ -2616,20 +2616,25 @@
 macfont_close (struct font *font)
 {
   struct macfont_info *macfont_info = (struct macfont_info *) font;
-  int i;
-
-  block_input ();
-  CFRelease (macfont_info->macfont);
-  CGFontRelease (macfont_info->cgfont);
-  if (macfont_info->screen_font)
-    CFRelease (macfont_info->screen_font);
-  macfont_release_cache (macfont_info->cache);
-  for (i = 0; i < macfont_info->metrics_nrows; i++)
-    if (macfont_info->metrics[i])
-      xfree (macfont_info->metrics[i]);
-  if (macfont_info->metrics)
-    xfree (macfont_info->metrics);
-  unblock_input ();
+
+  if (macfont_info->cache)
+    {
+      int i;
+
+      block_input ();
+      CFRelease (macfont_info->macfont);
+      CGFontRelease (macfont_info->cgfont);
+      if (macfont_info->screen_font)
+	CFRelease (macfont_info->screen_font);
+      macfont_release_cache (macfont_info->cache);
+      for (i = 0; i < macfont_info->metrics_nrows; i++)
+	if (macfont_info->metrics[i])
+	  xfree (macfont_info->metrics[i]);
+      if (macfont_info->metrics)
+	xfree (macfont_info->metrics);
+      macfont_info->cache = NULL;
+      unblock_input ();
+    }
 }
 
 static int