Replies: 1 comment
-
Discussed in and resolved by #5624 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a setup where I'm using a
std::weak_ptr
to keep track of objects that can originate in C++ or Python, and in a certain situation theweak_ptr
expires even if the corresponding Python object still exists. Am I doing something wrong or is there a Pybind11 issue? Any help will be appreciated.I have a minimum working example to demonstrate the issue. The C++ base class is
thing
, and there's a "holder" class that contains astd::weak_ptr< thing >
. I don't want the holder to influence the lifetime of thething
it holds, hence usingweak_ptr
. For the binding, I want to have a trampoline so I can have virtual functions handled properly in Python. For this example I've included a derivedthing
in Python calledDerivedThing
.The issue I'm running into is that after passing a
DerivedThing
tothing_holder
and returning to Python, when I try to access theDerivedThing
via its pointer inthing_holder
again, theweak_ptr
has expired.Here's the minimum working example. First the C++ classes:
Next, the trampoline class and Python bindings:
And finally the Python script that includes the derived thing class:
The expected output is:
The actual output is:
The issue is the last line, which indicates that the
weak_ptr
is no longer aware that the object still exists. My hypothesis is that even though we're usingpy::smart_holder
in thething
binding, the fact that we only keepweak_ptr< thing >
in the C++ results in the C++ side losing track of the Python objects, whereas if we had ashared_ptr< thing >
, according to the documentation, it would still work.I tested a these variations:
Thing
created in Python (as in the above example) -- worksDerivedThing
created in Python (as in the above example) -- does not workDerivedThing
created in Python with nothing_trampoline
-- works (except there's no virtual function overriding)DerivedThing
created in Python wherething_trampoline
also inherits frompy::trampoline_self_life_support
(commented out in the above code) -- does not workBeta Was this translation helpful? Give feedback.
All reactions