Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Overriding the pure virtual function result in trace trap error #714

Closed
umarfarook882 opened this issue Sep 10, 2024 · 2 comments
Closed

Comments

@umarfarook882
Copy link

Problem description

Overriding the pure virtual function in class result in trace trap error in python 3.12

Python: 3.12
nanobind: 2.1.0

Reproducible example code

#include <iostream>
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/trampoline.h>

class Base {
public:
    std::string name;
    Base(const std::string &name) : name(name) { 
        std::cout << "Base constructor" << std::endl;
    }
    virtual std::string bark() = 0;
};

class PyBase : public Base {
public:
    PyBase(const std::string &s) : Base(s) {
        std::cout << "PyBase constructor" << std::endl;
    }
    NB_TRAMPOLINE(Base, 1);

    std::string bark()  override {
        NB_OVERRIDE_PURE(bark);
    }
};

void alarm1(Base *base, size_t count = 3) {
    for (size_t i = 0; i < count; ++i)
        std::cout << base->bark() << std::endl;
}

NB_MODULE(my_ext, m)
{
    m.attr("__version__") = "0.0.1"; 
    nb::class_<Base, PyBase>(m, "Base")
        .def(nb::init<const std::string &>())
        .def("bark", &Base::bark); 
    m.def("alarm1", &alarm1, "base"_a, "count"_a = 3);
}
@wjakob
Copy link
Owner

wjakob commented Sep 12, 2024

I have doubts that there is a bug here, as the ability to override virtual calls (or not) is already tested on Python 3.12 by the CI test suite.

Your reproducer is missing the Python part.

What I suspect might be happening is that you overrode __init__ and did not call super().__init__().

@wjakob
Copy link
Owner

wjakob commented Sep 18, 2024

Closing due to a lack of response. Feel free to post here if there is more information, and I will reopen the PR.

@wjakob wjakob closed this as completed Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants