Subject: bug#56462: 29.0.50; [PATCH] Memory leak in ns_draw_relief

From: Daniel Martín <[email-masked]>
Date: Sat, 09 Jul 2022 16:13:39 +0200

I ran the Leaks tool with Emacs 29, and I've found a memory leak in the
NS version of Emacs.

Since commit c7b48b61d08f0b6a08584080badc60fe62ba1db1, in function
ns_draw_relief, static local variables baseCol and lightCol are assigned
to nil separately to their declaration.  That has the subtle consequence
that the further down calls to [baseCol release] and [lightCol release]
become a no-op, so each time ns_draw_relief is called it leaks some
instances of NSColor.

The fix is to revert to the previous way those static variables were
declared.

I've attached a patch with the correction.  With this fix, the leaks
tool doesn't report the NSColor leaks anymore when I play around with
Emacs 29.

Thanks.

>From acebb668e310da526f262f6a47090eddbb9c76a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <[email-masked]>
Date: Sat, 9 Jul 2022 15:54:07 +0200
Subject: [PATCH] Fix memory leak in ns_draw_relief

* src/nsterm.m (ns_draw_relief): Assigning the static local variables
baseCol and lightCol to nil leaks memory as of the second time
ns_draw_relief is called, because the calls to [baseCol release] and
[lightCol release] further down the function become a no-op.

This bug was introduced in c7b48b61d08f0b6a08584080badc60fe62ba1db1.
---
 src/nsterm.m | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index 7f232e7292..dc55edf0eb 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -3486,13 +3486,11 @@ larger if there are taller display elements (e.g., characters
     of some sides not being drawn, and because the rect will be filled.
    -------------------------------------------------------------------------- */
 {
-  static NSColor *baseCol, *lightCol, *darkCol;
+  static NSColor *baseCol = nil, *lightCol = nil, *darkCol = nil;
   NSColor *newBaseCol;
   NSRect inner;
   NSBezierPath *p;
 
-  baseCol = nil;
-  lightCol = nil;
   newBaseCol = nil;
   p = nil;
 
-- 
2.34.1