pybind / pybind/pybind11

[BUG] Segfault when calling a method chain

Open
#2,945 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Issue description

Python crashes when calling a method chain, see the example below.

Reproducible example code

I have three classes defined like this:

    py::class_<mr::Declension>(m_rus, "Declension")
        .def(py::init<>())
        .def("parse", [](const mr::Declension &self, const std::tstring & input, int attributes){
            std::unique_ptr<mr::Parse> res = self.parse(input, attributes);
            if (!res)
                throw std::domain_error("Parse error!");
            else 
                return res;
        }, py::arg("input"), py::arg("attributes") = 0));

    py::class_<mr::Paradigm>(m_rus, "Paradigm")
        .def_property_readonly("nominative", &mr::Paradigm::nominative)
        .def_property_readonly("genitive", &mr::Paradigm::genitive)
        .def_property_readonly("dative", &mr::Paradigm::dative)
        .def_property_readonly("accusative", &mr::Paradigm::accusative)
        .def_property_readonly("instrumental", &mr::Paradigm::instrumental)
        .def_property_readonly("prepositional", &mr::Paradigm::prepositional)
        .def_property_readonly("locative", &mr::Paradigm::locative);

    py::class_<mr::Parse, mr::Paradigm>(m_rus, "Parse")
        .def_property_readonly("plural", &mr::Parse::plural)
        .def_property_readonly("isAnimate", &mr::Parse::isAnimate)
        .def_property_readonly("paucal", &mr::Parse::getPaucal)
        .def_property_readonly("gender", &mr::Parse::gender);

Firing up Python:

C:\Code\morpher-cpp\python>python
Python 3.9.4 (tags/v3.9.4:1f2e308, Apr  4 2021, 13:27:16) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from morpher import russian as mr
>>> declension = mr.Declension()
>>> declension.parse("Иван").genitive
'Ивана'
>>> mr.Declension().parse("Иван")
<morpher.__morpher_rus.Parse object at 0x0000017C107094F0>
>>> mr.Declension().parse("Иван")
<morpher.__morpher_rus.Parse object at 0x0000017C10705D30>
>>> mr.Declension().parse("Иван").genitive
C:\Code\morpher-cpp\python>

The last Python command makes the Python interpreter crash.

I have tried returning a raw pointer from the "parse" method (res.release()) as well as returning a shared_ptr. The behaviour is the same.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Windows/Python 3.9 crash with the Declension.parse binding and the chained genitive access shown in the report. Trace the ownership and lifetime behavior across the returned Parse object and its Paradigm base, then verify that both the stored-object and temporary method-chain forms complete without a Python interpreter crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.