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.
-
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 importedAnimalUsage
class in my test file and tried to callget_animal
method:This call ends with segmentation fault:
In my source C++ example I keep
Frog
instance inAnimalUsage
class and return it from three different methods:getAnimal
,getAquaticAnimal
andgetFrog
. 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
Animal
copy constructor log I also noticed that object is being copied from address0x600000c80080
which is not the address ofAnimal
which has ben created. It corresponds to theFrog
address of the initial object. It seems to me that the copy should have been done from0x600000c800d8
address. It seems that pybind detects at the runtime that returnedAnimal&
object type isFrog
, goes to that object and uses it to callAnimal
copy 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
getAnimal
method.Thanks,
Davit
Beta Was this translation helpful? Give feedback.
All reactions