Skip to content

Commit

Permalink
Merge pull request #40492 from makortel/portableCollectionMemory
Browse files Browse the repository at this point in the history
Fix memory leaks in PortableHostCollection deserialization
  • Loading branch information
cmsbuild committed Feb 8, 2023
2 parents e22dce1 + 0eced58 commit 583e1a6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CUDADataFormats/PortableTestObjects/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
targetClass="cudatest::TestHostCollection"
version="[1-]"
source="portabletest::TestSoA layout_;"
target="buffer_"
target="buffer_,layout_,view_"
embed="false">
<![CDATA[
cudatest::TestHostCollection::ROOTReadStreamer(newObj, onfile.layout_);
Expand Down
3 changes: 2 additions & 1 deletion DataFormats/Portable/interface/PortableHostCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ class PortableHostCollection {
ConstBuffer const_buffer() const { return *buffer_; }

// part of the ROOT read streamer
static void ROOTReadStreamer(PortableHostCollection* newObj, Layout const& layout) {
static void ROOTReadStreamer(PortableHostCollection* newObj, Layout& layout) {
newObj->~PortableHostCollection();
// use the global "host" object returned by cms::alpakatools::host()
new (newObj) PortableHostCollection(layout.metadata().size(), cms::alpakatools::host());
newObj->layout_.ROOTReadStreamer(layout);
layout.ROOTStreamerCleaner();
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
targetClass="portabletest::TestHostCollection"
version="[1-]"
source="portabletest::TestSoA layout_;"
target="buffer_"
target="buffer_,layout_,view_"
embed="false">
<![CDATA[
portabletest::TestHostCollection::ROOTReadStreamer(newObj, onfile.layout_);
Expand Down
17 changes: 17 additions & 0 deletions DataFormats/SoATemplate/interface/SoALayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@

#define _VALUE_ELEMENT_INITIALIZERS(R, DATA, TYPE_NAME) BOOST_PP_EXPAND(_VALUE_ELEMENT_INITIALIZERS_IMPL TYPE_NAME)

/**
* Freeing of the ROOT-allocated column or scalar buffer
*/
// clang-format off
#define _ROOT_FREE_SOA_COLUMN_OR_SCALAR_IMPL(VALUE_TYPE, CPP_TYPE, NAME) \
delete[] BOOST_PP_CAT(NAME, _); \
BOOST_PP_CAT(NAME, _) = nullptr; \
// clang-format on

#define _ROOT_FREE_SOA_COLUMN_OR_SCALAR(R, DATA, TYPE_NAME) _ROOT_FREE_SOA_COLUMN_OR_SCALAR_IMPL TYPE_NAME

/**
* Computation of the column or scalar pointer location in the memory layout (at SoA construction time)
*/
Expand Down Expand Up @@ -565,6 +576,12 @@
_ITERATE_ON_ALL(_STREAMER_READ_SOA_DATA_MEMBER, ~, __VA_ARGS__) \
} \
\
/* ROOT allocation cleanup */ \
void ROOTStreamerCleaner() { \
/* This function should only be called from the PortableCollection ROOT streamer */ \
_ITERATE_ON_ALL(_ROOT_FREE_SOA_COLUMN_OR_SCALAR, ~, __VA_ARGS__) \
} \
\
/* Dump the SoA internal structure */ \
template <typename T> \
SOA_HOST_ONLY friend void dump(); \
Expand Down

0 comments on commit 583e1a6

Please sign in to comment.