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

[QUESTION] Implements a pure virtual class in python calls pure virtual function #2844

Open
jjcasmar opened this issue Feb 3, 2021 · 1 comment

Comments

@jjcasmar
Copy link

jjcasmar commented Feb 3, 2021

I am trying to implement a virtual class i have in C++ using Python. To do that, I am using the trampoline as described in pybind documentation. The issue here is that in python, I need to keep the python object around or I would have a pure virtual function call error.

My python class looks like this

class BlendingField(PyVNCS.Sim2D.BlendingField):
    def __init__(self):
        PyVNCS.Sim2D.BlendingField.__init__(self)

    def blending(self, point):
        return 0.5

And I have to use like this

blendingField = BlendingField()
simCreator.blendingField = blendingField

simCreator.create()

Basically, I create the BlendingField with the python implementation, I assign it to simCreator.blendingField property and I call simCreator.create(), which is a C++ function using the blending field.

If instead of doing it like that, I do it like this

simCreator.blendingField = BlendingField()

simCreator.create()

I get a crash when trying to use the blending field from C++. This happens because the python object is destroyed.

Is this the expected behavior? I find it kind of confusing and it needs an extra line which most of the times you dont really want.

@virtuald
Copy link
Contributor

virtuald commented Feb 3, 2021

Duplicate of #1120, #1145, #1333, #1389, #1552, #1546, #1566, #1774, #1902, #1937

Fixed in #2839, hopefully will be decided on/merged soon.

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