Repository navigation
fix: keep Python wrappers valid when ObjCryst++ releases sub-objects - #133
Conversation
Install ObjCryst++ ReleaseObj() hooks (vincefn/objcryst#106) so that sub-objects deleted internally by ObjCryst++ (reflection profiles, scatterers, scattering powers, refinable parameters, molecule atoms, restraints and rigid groups) no longer leave dangling Python wrappers. - If a Python wrapper exists, the hook hands ownership to it instead of letting ObjCryst++ delete the object; otherwise ObjCryst++ deletes it. - Wrappers of released RefinablePar objects are invalidated, since their value pointer refers to storage of the former owner. - Atoms pin their scattering power, and released restraints and rigid groups pin their atoms while the molecule is alive. - Remove the GetPar SetDeleteRefParInDestructor(0) workaround. - Crystal.RemoveScatterer, RemoveScatteringPower (when unused) and the Molecule.Remove* methods now delete unreferenced objects instead of leaking them. - Update extern/objcryst to a3d75af (vincefn-object-release-hook). Fixes #121, #132. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #133 +/- ##
==========================================
+ Coverage 98.80% 98.87% +0.07%
==========================================
Files 22 22
Lines 2343 2495 +152
==========================================
+ Hits 2315 2467 +152
Misses 28 28
🚀 New features to boost your workflow:
|
|
@sbillinge @SYMSCAE I let you check this - the hooks introduced are a bit long, but this should cover most cases. |
| { | ||
| pars[0] = &m.GetPar(&atom.X()); | ||
| pars[1] = &m.GetPar(&atom.Y()); | ||
| pars[2] = &m.GetPar(&atom.Z()); |
There was a problem hiding this comment.
are there any other atom attributes that have be treated the same way, such as ADPs and occupancies?
There was a problem hiding this comment.
In a Molecule we need this for the xyz atom coordinates because they are used as refinable parameters, so there are references pointing to them in the Molecule (not in a MolAtom because that class is not a RefinableObj). That's not the case for other parameters.
For ADPs these belong inside a ScatteringPower, and handled properly there.
Of course this is still a hook, the correct longer-term fix would be to implement reference counting in objcryst.
|
@SYMSCAE please could you look at this. Also, do we have any of the same issues with |
|
@SYMSCAE it would be great if you could look at this. However, I will merge it and push out an rc release and see what happens in the matrix..... |
|
OK, I pushed out |
|
LGTM. |
What problem does this PR address?
Fixes #121 and #132: ObjCryst++ internally deletes sub-objects (reflection profiles, refinable parameters, scatterers, scattering powers, molecule atoms/restraints) that Python wrappers still reference. The result was use-after-free, double free, or nanobind returning a stale wrapper of the wrong type for a new object allocated at the same address.
This relies on the
ReleaseObj()hook API added in vincefn/objcryst#106 (merged;extern/objcrystupdated to 0f8dfef).src/extensions/nb_release_hooks.cppinstalls the ObjCryst++ release hooks:RefinableParare invalidated (raiseTypeError), since their value points into the former owner's storage;Crystal.RemoveScatterer,Crystal.RemoveScatteringPower(when unused) andMolecule.Remove*now delete the removed objects instead of leaking them.GetParSetDeleteRefParInDestructor(0)workaround.Validation:
pytest tests— 214 passed (also passes underMallocScribble=1 MallocGuardEdges=1and with an AddressSanitizer build).SetInterpPoints,TextureMarchDollase.DeletePhase,ConnectAtoms,Crystal()destructor,RemoveScatterer,XMLInput,Molecule.RemoveAtom); 9 of them crash or fail onmain.Known remaining (pre-existing, not addressed here):
ScatteringPowerRegistry.GetObjreturns a wrapper at the unadjusted virtual-base address.What should the reviewer(s) do?
Review the hook design and the
Remove*behaviour change. Do not auto-merge.