Skip to content

Commit

Permalink
Fix a memory leak
Browse files Browse the repository at this point in the history
This commit adds freeing of memory allocated by ROOT during streaming from disk.
It also declares all members of PortableHostCollections as target (as the streamer fill
them all up).
  • Loading branch information
Eric Cano authored and makortel committed Jan 20, 2023
1 parent 8192b7f commit 0eced58
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_,layout_"
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 @@ -68,11 +68,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_,layout_"
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 @@ -266,6 +266,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 @@ -563,6 +574,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 0eced58

Please sign in to comment.