Skip to content

Commit 77bda84

Browse files
marsupialsftnight
authored andcommitted
Destroy elements in reverse order of construction.
Prevents possibility of last element referencing now invalid first element.
1 parent 2d141ff commit 77bda84

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/Interpreter/Value.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ namespace {
8686
assert (m_RefCnt > 0 && "Reference count is already zero.");
8787
if (--m_RefCnt == 0) {
8888
if (m_DtorFunc) {
89-
char* payload = getPayload();
90-
for (size_t el = 0; el < m_NElements; ++el)
91-
(*m_DtorFunc)(payload + el * m_AllocSize / m_NElements);
89+
assert(m_NElements && "No elements!");
90+
char* Payload = getPayload();
91+
const auto Skip = m_AllocSize / m_NElements;
92+
while (m_NElements-- != 0)
93+
(*m_DtorFunc)(Payload + m_NElements * Skip);
9294
}
9395
delete [] (char*)this;
9496
}

test/Interfaces/evaluate.C

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ gCling->evaluate("arrV", V);
208208

209209
V // CHECK-NEXT: (cling::Value &) boxes [(Tracer [3]) { @{{.*}}, @{{.*}}, @{{.*}} }]
210210

211+
// Explicitly destory the copies
212+
V = cling::Value()
213+
//CHECK-NEXT: MADE+{10}:dtor
214+
//CHECK-NEXT: MADE+{9}:dtor
215+
//CHECK-NEXT: MADE+{8}:dtor
216+
//CHECK-NEXT: (cling::Value &) <<<invalid>>> @0x{{.*}}
217+
218+
gCling->evaluate("arrV", V);
219+
//CHECK-NEXT: MADE+{11}:copy
220+
//CHECK-NEXT: MADE+{12}:copy
221+
//CHECK-NEXT: MADE+{13}:copy
222+
211223
// Destruct the variables with static storage:
212224
// Destruct arrV:
213225
//CHECK-NEXT: MADE{7}:dtor
@@ -217,7 +229,7 @@ V // CHECK-NEXT: (cling::Value &) boxes [(Tracer [3]) { @{{.*}}, @{{.*}}, @{{.*}
217229
// CHECK-NEXT: VAR{3}:dtor
218230
// CHECK-NEXT: REF{1}:dtor
219231

220-
//CHECK-NEXT: MADE+{8}:dtor
221-
//CHECK-NEXT: MADE+{9}:dtor
222-
//CHECK-NEXT: MADE+{10}:dtor
223-
232+
// V going out of scope
233+
//CHECK-NEXT: MADE+{13}:dtor
234+
//CHECK-NEXT: MADE+{12}:dtor
235+
//CHECK-NEXT: MADE+{11}:dtor

0 commit comments

Comments
 (0)