diff --git a/Framework/Core/include/Framework/DataAllocator.h b/Framework/Core/include/Framework/DataAllocator.h index 104e418cbcd19..0a6ac891e4f58 100644 --- a/Framework/Core/include/Framework/DataAllocator.h +++ b/Framework/Core/include/Framework/DataAllocator.h @@ -115,11 +115,30 @@ struct LifetimeHolder { // invoke the callback early (e.g. for the Product<> case) void release() { - if (ptr && callback) { - callback(*ptr); - delete ptr; - ptr = nullptr; + if (!ptr) { + return; } + + std::unique_ptr released{ptr}; + ptr = nullptr; + auto releaseCallback = std::move(callback); + if (!releaseCallback) { + return; + } + releaseCallback(*released); + } + + // Delete the owned object without invoking the release callback. This is used + // when a partially filled object must be abandoned. + void discard() + { + if (!ptr) { + return; + } + + callback = nullptr; + delete ptr; + ptr = nullptr; } }; diff --git a/Framework/Core/test/test_ASoA.cxx b/Framework/Core/test/test_ASoA.cxx index 48cfb277acc5c..d96304bd3bce2 100644 --- a/Framework/Core/test/test_ASoA.cxx +++ b/Framework/Core/test/test_ASoA.cxx @@ -1420,5 +1420,5 @@ TEST_CASE("TestWritingCursorLastIndexAndReserve") auto table = builder->finalize(); REQUIRE(table->num_rows() == 5); REQUIRE(table->num_columns() == 2); - delete builder; + cursor.release(); }