Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Introduce MaybeSharedPtr type and use it for mutable objects #220

Closed
wants to merge 5 commits into from

Conversation

tmadlener
Copy link
Collaborator

BEGINRELEASENOTES

  • Introduce MaybeSharedPtr<T> for usage in the mutable user facing classes via MaybeSharedPtr<Obj>.
  • Make the Obj* in Const classes unmanaged.
  • Make the clone function always return mutable objects (keeping the same relations as the original)
  • Fixes memory leak in SIOReader for collections that have not been requested by the user.
  • Remove ObjBase and move the ObjectID into the Obj classes.
  • Add possibility to build podio with different sanitizers via a cmake flag.
    • Add github actions workflow file for some of the sanitizer builds

ENDRELEASENOTES

Fixes #174

The MaybeSharedPtr holds the managed pointer as well as a simple control block which manages its own lifetime and potentially that of the managed pointer. Ownership of the managed pointer can only move from the MaybeSharedPtr to something else, but never the other way around (unless it has been initialized with an unmanaged pointer). This construct allows to avoid the issues in observed in #174, because the control block that decides how things should be destructed will still be around even if the managed pointer is already gone. Previously the managed pointer and the control block were one entity so this could not be guaranteed.

In order to avoid too much overhead it is only used in the mutable objects but not in the immutable ones; The size of the mutable user objects doubles, since it now has two pointers instead of just one, while the one of the immutable ones remains the same.

Another important difference: The immutable objects no longer manage their Obj* and always assume that someone else does this for them. This should not really be a problem as in most of the cases it will be the collection that owns the Obj*. However, it could introduce some subtle lifetime issues, that could occur when a mutable object is implicitly converted to an immutable one, e.g.

std::vector<ConstMCParticle> mcParticles;
{ // scope to illustrate the problem
  auto mcP = MCParticle();
  mcParticles.push_back(mcP);
}
// this will create a "dangling reference" that internally uses an already destroyed Obj*
auto mc = mcParticles[0];

Making the Const objects also use the MaybeSharedPtr could be done from a technical point of view, however, that leads to a problem with cyclic dependencies, e.g. here:

podio/tests/unittest.cpp

Lines 112 to 126 in ed207e8

TEST_CASE("Cyclic"){
auto start = ExampleForCyclicDependency1();
auto isAvailable = start.ref().isAvailable();
REQUIRE_FALSE(isAvailable);
auto end = ExampleForCyclicDependency2();
start.ref(end);
isAvailable = start.ref().isAvailable();
REQUIRE(isAvailable);
end.ref(start);
REQUIRE(start == end.ref());
auto end_eq = start.ref();
auto start_eq = end_eq.ref();
REQUIRE(start == start_eq);
REQUIRE(start == start.ref().ref());
}

Here something an additional "weak count" (a la std::shared_ptr + std::weak_ptr) would be necessary to break the cycle and ensure proper cleanup, otherwise the two references above keep each other alive indefinitely.

In the end, the lifetime issues around the Const objects could be acceptable, if properly documented. Otherwise we might have to find a more sophisticated approach than the one proposed here.

- Add a list of possible sanitizers that can be used to build the whole
  of podio with the USE_SANITIZER option
- At the moment only build podio but do not run the tests, since there
  are a few things that need to be fixed before they can be succesfully
  run with them
- Add a workflow file that builds podio with clang and gcc with all
  available sanitizers for the respective toolchain
Only mutable objects start with a MaybeSharedPtr<Obj> as Const objects
will (almost) always be managed. Introduces a few subtle lifetime issues
that have to be properly documented but should be OK for our usecases.
Making Const objbects managed via a MaybeSharedPtr too would be
possible, but introduces problems with cyclic dependencies
@tmadlener
Copy link
Collaborator Author

Looks like builds that involve any sanitizer are not really happy with root.

@@ -79,7 +80,10 @@ namespace podio {
void readMetaDataRecord(std::shared_ptr<SIONumberedMetaDataBlock> mdBlock);
void createBlocks();

typedef std::pair<CollectionBase*, std::string> Input;
/// collection, name, and flag to indictate whether it has been requested by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// collection, name, and flag to indictate whether it has been requested by
/// collection, name, and flag to indicate whether it has been requested by

@tmadlener
Copy link
Collaborator Author

Superseded by #514

@tmadlener tmadlener closed this Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

heap-use-after-free in ExampleCluster
2 participants