unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 56462@debbugs.gnu.org
Cc: luangruo@yahoo.com
Subject: bug#56462: 29.0.50; [PATCH] Memory leak in ns_draw_relief
Date: Sun, 10 Jul 2022 22:52:08 +0200	[thread overview]
Message-ID: <m11qusy9l3.fsf@yahoo.es> (raw)
In-Reply-To: <m1k08lxngj.fsf@yahoo.es> ("Daniel Martín via \"Bug reports for GNU Emacs, the Swiss army knife of text editors\""'s message of "Sun, 10 Jul 2022 12:37:48 +0200")

[-- Attachment #1: Type: text/plain, Size: 506 bytes --]

Daniel Martín via "Bug reports for GNU Emacs, the Swiss army knife of
text editors" <bug-gnu-emacs@gnu.org> writes:

>>
>> Thanks.  But I think the use of static variables there is rather ugly,
>> and it would be much nicer if we replicated the `x_setup_relief_color'
>> logic there.
>>
>> Do you want to work on that, or should I?
>
> Thanks, I can give it a try.

Here's a patch where the static variables are now proper fields in the
ns_output structure.  Is that what you had in mind?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-memory-leak-in-ns_draw_relief.patch --]
[-- Type: text/x-patch, Size: 4744 bytes --]

From ebcf7ca3e8a2add11050be4c1d7e991e3c0f0a6d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <mardani29@yahoo.es>
Date: Sun, 10 Jul 2022 22:36:28 +0200
Subject: [PATCH] Fix memory leak in ns_draw_relief

* src/nsterm.h (struct ns_output): New fields to store the relief
colors.
* src/nsterm.m (ns_setup_relief_colors): New function to keep the
relief colors as part of the ns_output structure.
(ns_draw_relief): Remove static local variables.  Assigning them to
nil caused a memory leak of NSColor instances (bug#56462).  Call
ns_setup_relief_colors instead.
---
 src/nsterm.h |  6 +++++
 src/nsterm.m | 68 +++++++++++++++++++++++++++-------------------------
 2 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/src/nsterm.h b/src/nsterm.h
index 7a097b3248..2a4c7571a3 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -927,6 +927,9 @@ #define KEY_NS_SHOW_PREFS              ((1<<28)|(0<<16)|14)
   NSColor *cursor_color;
   NSColor *foreground_color;
   NSColor *background_color;
+  NSColor *relief_background_color;
+  NSColor *light_relief_color;
+  NSColor *dark_relief_color;
   EmacsToolbar *toolbar;
 #else
   void *view;
@@ -934,6 +937,9 @@ #define KEY_NS_SHOW_PREFS              ((1<<28)|(0<<16)|14)
   void *cursor_color;
   void *foreground_color;
   void *background_color;
+  void *relief_background_color;
+  void *light_relief_color;
+  void *dark_relief_color;
   void *toolbar;
 #endif
 
diff --git a/src/nsterm.m b/src/nsterm.m
index 7f232e7292..8e0c4b84f0 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -3475,6 +3475,35 @@ larger if there are taller display elements (e.g., characters
     }
 }
 
+/* Set up colors for the relief lines around glyph string S.  */
+
+static void
+ns_setup_relief_colors (struct glyph_string *s)
+{
+  struct ns_output *di = FRAME_OUTPUT_DATA (s->f);
+  NSColor *color;
+
+  if (s->face->use_box_color_for_shadows_p)
+    color = [NSColor colorWithUnsignedLong: s->face->box_color];
+  else
+    color = [NSColor colorWithUnsignedLong: s->face->background];
+
+  if (s->hl == DRAW_CURSOR)
+    color = FRAME_CURSOR_COLOR (s->f);
+
+  if (color == nil)
+    color = [NSColor grayColor];
+
+  if (color != di->relief_background_color)
+    {
+      [di->relief_background_color release];
+      di->relief_background_color = [color retain];
+      [di->light_relief_color release];
+      di->light_relief_color = [[color highlightWithLevel: 0.4] retain];
+      [di->dark_relief_color release];
+      di->dark_relief_color = [[color shadowWithLevel: 0.4] retain];
+    }
+}
 
 static void
 ns_draw_relief (NSRect outer, int hthickness, int vthickness, char raised_p,
@@ -3486,40 +3515,13 @@ 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;
-  NSColor *newBaseCol;
   NSRect inner;
-  NSBezierPath *p;
-
-  baseCol = nil;
-  lightCol = nil;
-  newBaseCol = nil;
-  p = nil;
+  NSBezierPath *p = nil;
 
   NSTRACE ("ns_draw_relief");
 
   /* set up colors */
-
-  if (s->face->use_box_color_for_shadows_p)
-    newBaseCol = [NSColor colorWithUnsignedLong: s->face->box_color];
-  else
-    newBaseCol = [NSColor colorWithUnsignedLong: s->face->background];
-
-  if (s->hl == DRAW_CURSOR)
-    newBaseCol = FRAME_CURSOR_COLOR (s->f);
-
-  if (newBaseCol == nil)
-    newBaseCol = [NSColor grayColor];
-
-  if (newBaseCol != baseCol)  /* TODO: better check */
-    {
-      [baseCol release];
-      baseCol = [newBaseCol retain];
-      [lightCol release];
-      lightCol = [[baseCol highlightWithLevel: 0.4] retain];
-      [darkCol release];
-      darkCol = [[baseCol shadowWithLevel: 0.4] retain];
-    }
+  ns_setup_relief_colors (s);
 
   /* Calculate the inner rectangle.  */
   inner = outer;
@@ -3542,7 +3544,9 @@ larger if there are taller display elements (e.g., characters
   if (bottom_p)
     inner.size.height -= hthickness;
 
-  [(raised_p ? lightCol : darkCol) set];
+  struct ns_output *di = FRAME_OUTPUT_DATA (s->f);
+
+  [(raised_p ? di->light_relief_color : di->dark_relief_color) set];
 
   if (top_p || left_p)
     {
@@ -3564,7 +3568,7 @@ larger if there are taller display elements (e.g., characters
       [p fill];
     }
 
-  [(raised_p ? darkCol : lightCol) set];
+  [(raised_p ? di->dark_relief_color : di->light_relief_color) set];
 
   if (bottom_p || right_p)
     {
@@ -3626,7 +3630,7 @@ larger if there are taller display elements (e.g., characters
 				   NSMaxY (outer) - 0.5)];
     }
 
-  [darkCol set];
+  [di->dark_relief_color set];
   [p stroke];
 
   if (vthickness > 1 && hthickness > 1)
-- 
2.34.1


  reply	other threads:[~2022-07-10 20:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <m1fsjaz84s.fsf.ref@yahoo.es>
2022-07-09 14:13 ` bug#56462: 29.0.50; [PATCH] Memory leak in ns_draw_relief Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-07-10  2:25   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-07-10 10:37     ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-07-10 20:52       ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-07-11  1:50         ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-07-11  8:01           ` Alan Third
2022-07-11 10:25         ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-05 19:23           ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m11qusy9l3.fsf@yahoo.es \
    --to=bug-gnu-emacs@gnu.org \
    --cc=56462@debbugs.gnu.org \
    --cc=luangruo@yahoo.com \
    --cc=mardani29@yahoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).