diff options
author | Daniel MartÃn <mardani29@yahoo.es> | 2022-07-10 22:36:28 +0200 |
---|---|---|
committer | Po Lu <luangruo@yahoo.com> | 2022-07-11 18:21:43 +0800 |
commit | 7ae7a95e803aa898d31673d552f254092ce202b1 (patch) | |
tree | 1803da41d924d1e7e9f0ce6010fd99cbe82a4118 | |
parent | bebf39f292f1963ab980497248a18d8035708d1a (diff) | |
download | emacs-7ae7a95e803aa898d31673d552f254092ce202b1.tar.gz |
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.
-rw-r--r-- | src/nsterm.h | 6 | ||||
-rw-r--r-- | src/nsterm.m | 68 |
2 files changed, 42 insertions, 32 deletions
diff --git a/src/nsterm.h b/src/nsterm.h index 7a097b32489..2a4c7571a34 100644 --- a/src/nsterm.h +++ b/src/nsterm.h @@ -927,6 +927,9 @@ struct ns_output 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 @@ struct ns_output 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 7f232e72922..8e0c4b84f0e 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -3475,6 +3475,35 @@ ns_draw_box (NSRect r, CGFloat hthickness, CGFloat vthickness, } } +/* 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 @@ ns_draw_relief (NSRect outer, int hthickness, int vthickness, char raised_p, 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 @@ ns_draw_relief (NSRect outer, int hthickness, int vthickness, char raised_p, 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 @@ ns_draw_relief (NSRect outer, int hthickness, int vthickness, char raised_p, [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 @@ ns_draw_relief (NSRect outer, int hthickness, int vthickness, char raised_p, NSMaxY (outer) - 0.5)]; } - [darkCol set]; + [di->dark_relief_color set]; [p stroke]; if (vthickness > 1 && hthickness > 1) |