Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 8e404e3

Browse files
committed
[[ Bug 22017 ]] Fix memory leak when updating listBehavior fields
This patch fixes a memory leak which can occur when updating listBehavior fields in some cases. The leak was caused by failing to release the saved set of previously hilited lines after re-applying them. It has been fixed by passing a reference to an appropriate auto-class around, rather than bare pointers.
1 parent b5e0e6b commit 8e404e3

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

docs/notes/bugfix-22017.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix memory leak when updating listBehavior fields in some cases

engine/src/field.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class MCField : public MCControl, public MCMixinObjectHandle<MCField>
457457

458458
Exec_stat seltext(findex_t si, findex_t ei, Boolean focus, Boolean update = False);
459459
uint2 hilitedline();
460-
void hilitedlines(vector_t<uint32_t> &r_lines);
460+
void hilitedlines(MCAutoArray<uint32_t>& r_lines);
461461
Exec_stat sethilitedlines(const uint32_t *p_lines, uint32_t p_line_count, Boolean forcescroll = True);
462462
void hiliteline(int2 x, int2 y);
463463

engine/src/fieldf.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,23 +337,15 @@ void MCField::resetparagraphs()
337337
findex_t si = 0;
338338
findex_t ei = 0;
339339

340-
vector_t<uint32_t> t_lines;
341-
t_lines . elements = nil;
342-
t_lines . count = 0;
340+
MCAutoArray<uint32_t> t_lines;
341+
343342
// MW-2005-05-13: [[Fix bug 2766]] We always need to retrieve the hilitedLines
344343
// to prevent phantom selections w.r.t. focused paragraph.
345344
if (flags & F_LIST_BEHAVIOR)
346345
hilitedlines(t_lines);
347346

348347
if (MCactivefield == this && focusedparagraph != NULL)
349348
{
350-
// TS-2005-01-06: Fix for bug 2381
351-
// A) get old hilites lines
352-
// B) clear current hilites line, useless now, but may actually do something
353-
// in the future
354-
if (flags & F_LIST_BEHAVIOR)
355-
hilitedlines(t_lines);
356-
357349
selectedmark(False, si, ei, True);
358350
if (flags & F_LIST_BEHAVIOR)
359351
sethilitedlines(NULL, 0);
@@ -373,9 +365,9 @@ void MCField::resetparagraphs()
373365
// MW-2005-01-28: Correct small integration error, != instead of ==
374366
if ((flags & F_LIST_BEHAVIOR) != 0)
375367
{
376-
if (t_lines . elements != nil)
368+
if (t_lines.Ptr() != nil)
377369
{
378-
sethilitedlines(t_lines . elements, t_lines . count, False);
370+
sethilitedlines(t_lines.Ptr(), t_lines.Size(), False);
379371
}
380372
}
381373
else if (ei != 0)

engine/src/fields.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -996,30 +996,24 @@ uint2 MCField::hilitedline()
996996
return line;
997997
}
998998

999-
void MCField::hilitedlines(vector_t<uint32_t> &r_lines)
999+
void MCField::hilitedlines(MCAutoArray<uint32_t>& r_lines)
10001000
{
1001-
if (r_lines.elements != nil)
1002-
delete r_lines.elements;
1003-
if (r_lines.count != 0)
1004-
r_lines.count = 0;;
10051001
if (!opened || !(flags & F_LIST_BEHAVIOR))
10061002
return;
1003+
10071004
uinteger_t line = 0;
1008-
MCAutoArray<uint32_t> t_lines;
10091005

10101006
MCParagraph *pgptr = paragraphs;
10111007
do
10121008
{
10131009
line++;
10141010
if (pgptr->gethilite())
10151011
{
1016-
/* UNCHECKED */ t_lines . Push(line);
1012+
/* UNCHECKED */ r_lines . Push(line);
10171013
}
10181014
pgptr = pgptr->next();
10191015
}
10201016
while (pgptr != paragraphs);
1021-
1022-
t_lines . Take(r_lines . elements, r_lines . count);
10231017
}
10241018

10251019
Exec_stat MCField::sethilitedlines(const uint32_t *p_lines, uint32_t p_line_count, Boolean forcescroll)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
script "CoreFieldListBehavior"
2+
/*
3+
Copyright (C) 2019 LiveCode Ltd.
4+
5+
This file is part of LiveCode.
6+
7+
LiveCode is free software; you can redistribute it and/or modify it under
8+
the terms of the GNU General Public License v3 as published by the Free
9+
Software Foundation.
10+
11+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
12+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
18+
19+
on TestListBehaviorHilitedLines
20+
/* the hilitedLines of a field only work if the field is open so open
21+
* this stack */
22+
go this stack
23+
24+
reset the templateField
25+
create field "Test"
26+
set the dontWrap of field "Test" to true
27+
set the autoHilite of field "Test" to true
28+
set the listBehavior of field "Test" to true
29+
set the nonContiguousHilites of field "Test" to true
30+
set the multipleHilites of field "Test" to true
31+
set the text of field "Test" to format("first\nsecond\nthird\nfourth\nfifth")
32+
33+
TestAssert "hilitedLines start empty", the hilitedLines of field "Test" is empty
34+
35+
set the hilitedLines of field "Test" to "2,4"
36+
TestAssert "hilitedLines roundtrip", the hilitedLines of field "Test" is "2,4"
37+
38+
set the width of field "Test" to the width of field "Test" * 2
39+
set the hilitedLines of field "Test" to "2,4"
40+
TestAssert "hilitedLines survive field resize", the hilitedLines of field "Test" is "2,4"
41+
42+
set the hilitedLines of field "Test" to "2,4"
43+
focus on field "Test"
44+
focus on nothing
45+
TestAssert "hilitedLines survive focus out", the hilitedLines of field "Test" is "2,4"
46+
47+
set the hilitedLines of field "Test" to "2,4"
48+
focus on field "Test"
49+
TestAssert "hilitedLines survive focus in", the hilitedLines of field "Test" is "2,4"
50+
end TestListBehaviorHilitedLines

0 commit comments

Comments
 (0)