jacg / nain4

An API that makes it easier to write Geant4 application code.
https://jacg.github.io/nain4/
2 stars 3 forks source link

Modern C++ resource ownership and lifetime management #35

Open jacg opened 1 year ago

jacg commented 1 year ago

Modern (since C++11) practice dictates that

In more detail

Things like G4PVPlacement (consequently n4::place), the subclasses of G4VSolid, G4LogicalVolume (thus n4::volume) all implicitly give ownership to some G4 manager in their constructors. So it is correct that the n4 utilities that wrap them, return bare pointers: you can look at this and use it but do not delete it, because it's not yours. So this seems to be consistent with modern C++.

G4RunManagerFactory unfortunately returns a bare pointer, and most users are unaware of it, and construct the run manager directly; in both cases, they end up having to remember to delete the manager by hand at the end. We already have n4::run_manager which mitigates this problem.

We need to hunt down these sorts of situations and

jacg commented 9 months ago

So far, the run manager seems to be the only thing the user owns. Everything else in G4 seems to follow the model "Create a new one and drop it on the floor, G4 will store it in some G4 store" (has a poetic ring to it).

Let's keep our eye out for a little longer, but I suspect there is not much we can do here.