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
Uh oh!
There was an error while loading. Please reload this page.
-
I have a setup where I'm using a
std::weak_ptrto keep track of objects that can originate in C++ or Python, and in a certain situation theweak_ptrexpires 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 thethingit 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 derivedthingin Python calledDerivedThing.The issue I'm running into is that after passing a
DerivedThingtothing_holderand returning to Python, when I try to access theDerivedThingvia its pointer inthing_holderagain, theweak_ptrhas 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_ptris no longer aware that the object still exists. My hypothesis is that even though we're usingpy::smart_holderin thethingbinding, 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:
Thingcreated in Python (as in the above example) -- worksDerivedThingcreated in Python (as in the above example) -- does not workDerivedThingcreated in Python with nothing_trampoline-- works (except there's no virtual function overriding)DerivedThingcreated in Python wherething_trampolinealso inherits frompy::trampoline_self_life_support(commented out in the above code) -- does not workBeta Was this translation helpful? Give feedback.
All reactions