Copying from wrong address + segmentation fault issue #3506
Replies: 2 comments
-
|
Two things.
animalUsage.def("get_animal", &example::AnimalUsage::getAnimal, py::return_value_policy::reference_internal);
animalUsage.def("get_aquatic_animal", &example::AnimalUsage::getAquaticAnimal, py::return_value_policy::reference_internal);
animalUsage.def("get_frog", &example::AnimalUsage::getFrog, py::return_value_policy::reference_internal);That
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks @jagerman, After noticing this issue, I started to use reference_internal policy, but yet wanted to understand the source of the issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
Recently I have encountered with the situation (segmentation fault issue) which is difficult for me to debug.
I have a diamond-like hierarchy of classes in C++, which I want to use in python. I have written pybind wrapper but
had some problems after compiling and trying to call one of my methods.
Here is the C++ source code (animal.hpp):
Corresponding pybind wrapper:
After compiling this file and generating the lib by the name
examples, I importedAnimalUsageclass in my test file and tried to callget_animalmethod:This call ends with segmentation fault:
In my source C++ example I keep
Froginstance inAnimalUsageclass and return it from three different methods:getAnimal,getAquaticAnimalandgetFrog. I know that I shouldn't expect polymorphism to work here since the default return value policy is copy policy for references, but I can't find out the cause of segmentation fault.Except of segfault issue, from
Animalcopy constructor log I also noticed that object is being copied from address0x600000c80080which is not the address ofAnimalwhich has ben created. It corresponds to theFrogaddress of the initial object. It seems to me that the copy should have been done from0x600000c800d8address. It seems that pybind detects at the runtime that returnedAnimal&object type isFrog, goes to that object and uses it to callAnimalcopy constructor.I want to know whether those kind of issues are known, is the copy address correct and what causes segfault issue ?
FYI: everything is ok when I return by value instead of returning by reference from
getAnimalmethod.Thanks,
Davit
Beta Was this translation helpful? Give feedback.
All reactions