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

Commit 0b424e2

Browse files
committed
[[ Bug 21887 ]] Use auto classes to manage MCTextChunkIterator instances
This patch replaces the use of explicit deallocation of MCTextChunkIterator instances with auto classes. By doing this memory leaks in the LCB 'is among the chars of' and 'the number of chars in' operators are eliminated.
1 parent 009e465 commit 0b424e2

File tree

5 files changed

+15
-22
lines changed

5 files changed

+15
-22
lines changed

docs/notes/bugfix-21887.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Fix memory leaks in LCB 'number of chars in' and 'is among the chars of' operators
2+

engine/src/exec-keywords.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ void MCKeywordsExecRepeatFor(MCExecContext& ctxt, MCStatement *statements, MCExp
417417
index_t t_sequenced_iterator;
418418
const byte_t *t_data_ptr;
419419

420-
MCTextChunkIterator *tci = nil;
420+
MCAutoPointer<MCTextChunkIterator> tci;
421421

422422
if (!ctxt . TryToEvaluateExpression(endcond, line, pos, EE_REPEAT_BADFORCOND, &t_condition))
423423
return;
@@ -551,7 +551,7 @@ void MCKeywordsExecRepeatFor(MCExecContext& ctxt, MCStatement *statements, MCExp
551551

552552
default:
553553
{
554-
t_found = MCStringsTextChunkIteratorNext(ctxt, tci);
554+
t_found = MCStringsTextChunkIteratorNext(ctxt, *tci);
555555
endnext = tci -> IsExhausted();
556556

557557
if (!t_found)
@@ -595,8 +595,6 @@ void MCKeywordsExecRepeatFor(MCExecContext& ctxt, MCStatement *statements, MCExp
595595

596596
done = done || endnext;
597597
}
598-
599-
delete tci;
600598
}
601599

602600
void MCKeywordsExecRepeatWith(MCExecContext& ctxt, MCStatement *statements, MCExpression *step, MCExpression *startcond, MCExpression *endcond, MCVarref *loopvar, real8 stepval, uint2 line, uint2 pos)

engine/src/exec-strings-chunk.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ void MCStringsCountChunksInRange(MCExecContext& ctxt, Chunk_term p_chunk_type, M
6464
return;
6565
}
6666

67-
MCTextChunkIterator *tci;
67+
MCAutoPointer<MCTextChunkIterator> tci;
6868
tci = MCStringsTextChunkIteratorCreateWithRange(ctxt, p_string, p_range, p_chunk_type);
6969

7070
r_count = tci -> CountChunks();
7171

72-
delete tci;
7372
return;
7473
}
7574

@@ -1145,9 +1144,7 @@ MCTextChunkIterator *MCStringsTextChunkIteratorCreate(MCExecContext& ctxt, MCStr
11451144
{
11461145
if (p_chunk_type == CT_TOKEN)
11471146
{
1148-
MCTextChunkIterator *tci;
1149-
tci = new (nothrow) MCTextChunkIterator_Tokenized(p_text, MCChunkTypeFromChunkTerm(p_chunk_type));
1150-
return tci;
1147+
return new (nothrow) MCTextChunkIterator_Tokenized(p_text, MCChunkTypeFromChunkTerm(p_chunk_type));
11511148
}
11521149

11531150
return MCChunkCreateTextChunkIterator(p_text, nil, MCChunkTypeFromChunkTerm(p_chunk_type), ctxt . GetLineDelimiter(), ctxt . GetItemDelimiter(), ctxt . GetStringComparisonType());
@@ -1157,9 +1154,7 @@ MCTextChunkIterator *MCStringsTextChunkIteratorCreateWithRange(MCExecContext& ct
11571154
{
11581155
if (p_chunk_type == CT_TOKEN)
11591156
{
1160-
MCTextChunkIterator *tci;
1161-
tci = new (nothrow) MCTextChunkIterator_Tokenized(p_text, MCChunkTypeFromChunkTerm(p_chunk_type), p_range);
1162-
return tci;
1157+
return new (nothrow) MCTextChunkIterator_Tokenized(p_text, MCChunkTypeFromChunkTerm(p_chunk_type), p_range);
11631158
}
11641159

11651160
return MCChunkCreateTextChunkIterator(p_text, &p_range, MCChunkTypeFromChunkTerm(p_chunk_type), ctxt . GetLineDelimiter(), ctxt . GetItemDelimiter(), ctxt . GetStringComparisonType());

engine/src/exec-strings.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,13 +1531,12 @@ bool MCStringsEvalIsAmongTheChunksOf(MCExecContext& ctxt, MCStringRef p_chunk, M
15311531
MCChunkType t_type;
15321532
t_type = MCChunkTypeFromChunkTerm(p_chunk_type);
15331533

1534-
MCTextChunkIterator *tci;
1535-
tci = MCStringsTextChunkIteratorCreate(ctxt, p_text, p_chunk_type);
1534+
MCAutoPointer<MCTextChunkIterator> tci =
1535+
MCStringsTextChunkIteratorCreate(ctxt, p_text, p_chunk_type);
15361536

15371537
bool t_result;
15381538
t_result = tci -> IsAmong(p_chunk);
15391539

1540-
delete tci;
15411540
return t_result;
15421541

15431542
}
@@ -1680,12 +1679,11 @@ __MCStringsEvalChunkOffset(MCExecContext& ctxt,
16801679
MCChunkType t_type;
16811680
t_type = MCChunkTypeFromChunkTerm(p_chunk_type);
16821681

1683-
MCTextChunkIterator *tci;
1684-
tci = MCStringsTextChunkIteratorCreate(ctxt, p_string, p_chunk_type);
1682+
MCAutoPointer<MCTextChunkIterator> tci =
1683+
MCStringsTextChunkIteratorCreate(ctxt, p_string, p_chunk_type);
16851684

16861685
uindex_t t_offset = tci -> ChunkOffset(p_chunk, p_start_offset, nil, ctxt . GetWholeMatches());
16871686

1688-
delete tci;
16891687
return t_offset;
16901688
}
16911689

libscript/src/module-char.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ bool MCCharStoreChunk(MCStringRef &x_target, MCStringRef p_value, MCRange p_grap
4848

4949
extern "C" MC_DLLEXPORT_DEF void MCCharEvalNumberOfCharsIn(MCStringRef p_target, index_t& r_output)
5050
{
51-
MCTextChunkIterator *tci;
52-
tci = MCChunkCreateTextChunkIterator(p_target, nil, kMCChunkTypeCharacter, nil, nil, kMCStringOptionCompareExact);
51+
MCAutoPointer<MCTextChunkIterator> tci =
52+
MCChunkCreateTextChunkIterator(p_target, nil, kMCChunkTypeCharacter, nil, nil, kMCStringOptionCompareExact);
5353
r_output = tci -> CountChunks();
5454
}
5555

@@ -65,8 +65,8 @@ extern "C" MC_DLLEXPORT_DEF void MCCharEvalIsAmongTheCharsOf(MCStringRef p_needl
6565
return;
6666
}
6767

68-
MCTextChunkIterator *tci;
69-
tci = MCChunkCreateTextChunkIterator(p_target, nil, kMCChunkTypeCharacter, nil, nil, kMCStringOptionCompareExact);
68+
MCAutoPointer<MCTextChunkIterator> tci =
69+
MCChunkCreateTextChunkIterator(p_target, nil, kMCChunkTypeCharacter, nil, nil, kMCStringOptionCompareExact);
7070
r_output = tci -> IsAmong(p_needle);
7171
}
7272

0 commit comments

Comments
 (0)