pybind / pybind/pybind11

[BUG]: Unexpected behavior assigning Python callback to C++ class

Open
#5,466 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

741d86f2e3527b667ba85d273a5eea19a0978ef5

Problem description

I'm trying to store a Python callback within a C++ object so that it can be invoked later but am noticing that the containing class is not properly destroyed on cleanup. The destructor for Container is never run, but only when the Python callback object is copied into the container. Using a reference or pointer member variable for the Python callback object appears to avoid this issue,

Reproducible example code
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>

#include <iostream>

using Callback = std::function<void(int)>;

class Container {
public:
    Container(Callback& cb) : mCallback(cb)
    {
        std::cout << "Container::Container()" << std::endl;
    }
    ~Container()
    {
        std::cout << "Container::~Container()" << std::endl;
    }
private:
    Callback mCallback;
};

PYBIND11_MODULE(container, m) {
    pybind11::class_<Container>(m, "Container")
		.def(pybind11::init<Callback&>());
}
import container

def cb():
    pass

c = container.Container(cb)

The following works correctly, Container::~Container() is run

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>

#include <iostream>

using Callback = std::function<void(int)>;

class Container {
public:
    Container(Callback& cb) : mCallback(cb)
    {
        std::cout << "Container::Container()" << std::endl;
    }
    ~Container()
    {
        std::cout << "Container::~Container()" << std::endl;
    }
private:
    Callback& mCallback;
};

PYBIND11_MODULE(container, m) {
    pybind11::class_<Container>(m, "Container")
		.def(pybind11::init<Callback&>());
}
Is this a regression? Put the last known working version here if it is.

Not a regression

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

The issue names no repository files or tests. Start by running the provided C++/Python reproducer and tracing the pybind11 callback conversion and object-lifetime path. Done means identifying why copying the callback prevents Container destruction and adding a regression check that confirms cleanup occurs.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.