Skip to content

fix: lifetime management regressions in nanobind bindings #123

Closed

Description

@SYMSCAE

Problem

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.

Activity

  1. vincefn commented on Nov 4, 2026

    @vincefn
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions