You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When overriding...the missing __init__ to parent very common bug. #96
If you are defining a custom constructor in a derived Python class, you must ensure that you explicitly call the bound C++ constructor using init, regardless of whether it is a default constructor or not. Otherwise, the memory for the C++ portion of the instance will be left uninitialized, which will generally leave the C++ instance in an invalid state and cause undefined behavior if the C++ instance is subsequently used.
Here is an example:
classDachschund(Dog):
def__init__(self, name):
Dog.__init__(self) # Without this, undefind behavior may occur if the C++ portions are referenced.self.name=namedefbark(self):
return"yap!"
Note that a direct init constructor should be called, and super() should not be used. For simple cases of linear inheritance, super() may work, but once you begin mixing Python and C++ multiple inheritance, things will fall apart due to differences between Python’s MRO and C++’s mechanisms.
Activity
marques-bruno commentedon Sep 11, 2019
From the pybind11 documentation:
I don't think it's avoidable
damienmarchal commentedon Sep 11, 2019
I know..seems hard to avoid but having a clean way to warn users how to fix the crash would be an already nice objective.