You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: lifetime management regressions in nanobind bindings #123
The nanobind_migration_test branch has lifetime management bug:
Atom does not retain its ScatteringPower. The ctor and Init omit the lifetime protection present in the Boost.Python bindings. If the scattering power is destroyed first, Atom::~Atom() accesses freed memory: AddressSanitizer: heap-use-after-free ObjCryst::Atom::~Atom() — Atom.cpp:105 mpScattPowAtom->DeRegisterClient(*this)
This was discovered in diffpy.srreal's tests.
Proposed solution
Add nb::keep_alive to Atom ctor, its copy ctor, and Init.
Thanks, that's exactly the kind of issues we have to look for in this migration.
btw, I'm still dreaming of using ObjCryst's RegisterClient() to update python's reference counting directly, rather than using these python/c++ lifetime hooks, but I'm not sure if this would be easily doable.
Activity
vincefn commentedon Nov 4, 2026
Thanks, that's exactly the kind of issues we have to look for in this migration.
btw, I'm still dreaming of using ObjCryst's
RegisterClient()to update python's reference counting directly, rather than using these python/c++ lifetime hooks, but I'm not sure if this would be easily doable.test: add Atom/ScatteringPower lifetime tests (diffpy#123)
Fix lifetime management regressions in Atom bindings (#124)