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.
This commit is contained in:
Daniel Martín 2022-07-10 22:36:28 +02:00 committed by Po Lu
parent bebf39f292
commit 7ae7a95e80
2 changed files with 42 additions and 32 deletions

View file

@ -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

View file

@ -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)