Skip to content

make_iterator throws exception after re-initializing interpreter #2101

Closed
@bkloster-sma

Description

@bkloster-sma

Issue description

  1. Initialize an interpreter
  2. Call make_iterator on some begin/end pair of a container
  3. Finalize interpreter, initialize it again
  4. Call make_iterator again, same as above

Result: An exception is thrown "instance allocation failed: new instance has no pybind11-registered base types".

Reproducible example code

    std::vector<int> container;

    // Initialize an interpreter
    pybind11::initialize_interpreter();

    // Call make_iterator
    {
        pybind11::gil_scoped_acquire gil;
        auto iter = pybind11::make_iterator(container.begin(), container.end());
    }

    // Finalizer interpreter, initialize it again
    pybind11::finalize_interpreter();
    pybind11::initialize_interpreter();

    // Call make iterator again
    {
        pybind11::gil_scoped_acquire gil;
        auto iter = pybind11::make_iterator(container.begin(), container.end());
    }
    pybind11::finalize_interpreter();

Proposed Fix

The culprit seems to be static state in pybind11::detail::registered_local_types_cpp(). Clearing that container (deleting the heap-allocated values in it beforehand) in finalize_interpreter() appears to fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions