Skip to content

Commit 31f5f10

Browse files
authored
DPL: add discard support for LifetimeHolder (#15592)
1 parent 7fb5f59 commit 31f5f10

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

Framework/Core/include/Framework/DataAllocator.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,30 @@ struct LifetimeHolder {
115115
// invoke the callback early (e.g. for the Product<> case)
116116
void release()
117117
{
118-
if (ptr && callback) {
119-
callback(*ptr);
120-
delete ptr;
121-
ptr = nullptr;
118+
if (!ptr) {
119+
return;
122120
}
121+
122+
std::unique_ptr<T> released{ptr};
123+
ptr = nullptr;
124+
auto releaseCallback = std::move(callback);
125+
if (!releaseCallback) {
126+
return;
127+
}
128+
releaseCallback(*released);
129+
}
130+
131+
// Delete the owned object without invoking the release callback. This is used
132+
// when a partially filled object must be abandoned.
133+
void discard()
134+
{
135+
if (!ptr) {
136+
return;
137+
}
138+
139+
callback = nullptr;
140+
delete ptr;
141+
ptr = nullptr;
123142
}
124143
};
125144

Framework/Core/test/test_ASoA.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,5 +1420,5 @@ TEST_CASE("TestWritingCursorLastIndexAndReserve")
14201420
auto table = builder->finalize();
14211421
REQUIRE(table->num_rows() == 5);
14221422
REQUIRE(table->num_columns() == 2);
1423-
delete builder;
1423+
cursor.release();
14241424
}

0 commit comments

Comments
 (0)