-
Notifications
You must be signed in to change notification settings - Fork 72
Description
I'm opening this issue following the discussion in
https://discourse.julialang.org/t/object-lifetime-problem-with-cxxwrap-and-make-const-array/40991
In short: Some C++ APIs require that different objects are kept alive together, even though they don't explicitly depend on each other.
In long: I can't comment on the other use case in the discourse thread, but I found this to be the case for the classes I'm wrapping in https://github.com/jstrube/FastJet.jl, which wraps http://fastjet.fr/. Specifically, there is a class ClusterSequence, which can create a PseudoJet. The latter optionally contains a pointer to the ClusterSequence that created it, and accessing certain properties only works if both are still in scope. There is no way for julia to know this, however, and there are situations in which the ClusterSequence is garbage-collected, before the last PseudoJet it created goes out of scope. In that case, accessing some properties of those PseudoJets causes a segfault.
One way to fix this currently is to make changes like this one JuliaHEP/FastJet.jl@a229530, but this exposes annoying details about the C++ library to the Julia users and it isn't very pretty.
Another way could be if there were a way to signal this relationship between objects, so that e.g. ClusterSequence objects don't get cleaned up if they still have dependent PseudoJet objects in scope.
In my opinion, this should actually just be fixed on the C++ side by changing the design of the classes and just giving the PseudoJet ownership of the information that it needs, and I will file an issue about this, but the fact that this discussion was initiated by a third party suggests that other C++ packages might have similar issues.