Subject: Re: bug#21556: 25.0.50; Memory leak in emacs -Q with lucid (font)

From: Dima Kogan <[email-masked]>
Date: Tue, 29 Sep 2015 02:28:56 -0700

Eli Zaretskii <[email-masked]> writes:

> So, if you prefer not to invest any time in investigating the font
> marking code, let's wait for Dmitry to respond, and make the decision
> when he does (or when we give up waiting).

I guess I preferred to invest more time. I found and fixed the bug, and
the patch is attached. The issue was that the compaction code wasn't
checking all the right lisp objects for the marks. The font entities
were storing a list of fonts, and this list had to be traversed, looking
for the marks. See font_clear_cache() for a function that WAS traversing
the full list.

I'm certain there are more leaks here, but bug 21509 is now the main
leaker with my current test case.

>From e9b0394826c4e706550259cd3862a89343c6cf2b Mon Sep 17 00:00:00 2001
From: Dima Kogan <[email-masked]>
Date: Tue, 29 Sep 2015 02:19:35 -0700
Subject: [PATCH] compact_font_cache_entry() now properly checks for marked
 fonts

* src/alloc.c (compact_font_cache_entry): When checking for marked
fonts we were looking at a font entity object.  However the entity
could contain a list of font objects that need to be checked, which we
now do.  This resolves a memory leak
Fixes: bug#21556
---
 src/alloc.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index 3ab2a6e..03df258 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -5303,8 +5303,31 @@ compact_font_cache_entry (Lisp_Object entry)
 	     are not marked too.  But we must be sure that nothing is
 	     marked within OBJ before we really drop it.  */
 	  for (i = 0; i < size; i++)
-	    if (VECTOR_MARKED_P (XFONT_ENTITY (AREF (XCDR (obj), i))))
-	      break;
+            {
+              Lisp_Object objlist;
+              bool any_fonts_marked;
+
+              if (VECTOR_MARKED_P (XFONT_ENTITY (AREF (XCDR (obj), i))))
+                break;
+
+              objlist = AREF (AREF (XCDR (obj), i), FONT_OBJLIST_INDEX);
+              any_fonts_marked = false;
+
+              for (; CONSP (objlist); objlist = XCDR (objlist))
+                {
+                  Lisp_Object val = XCAR (objlist);
+                  struct font *font = XFONT_OBJECT (val);
+
+                  if (! NILP (AREF (val, FONT_TYPE_INDEX)) &&
+                      VECTOR_MARKED_P(font))
+                    {
+                      any_fonts_marked = true;
+                      break;
+                    }
+                }
+              if(any_fonts_marked)
+                break;
+            }
 
 	  if (i == size)
 	    drop = 1;
-- 
2.1.4