[C++-sig] bases + wrapper bug?
roman.yakovenko at gmail.com
Sat Aug 18 19:54:55 CEST 2007
On 8/17/07, Piotr Jaroszyński <p.jaroszynski at gmail.com> wrote:
> I might have hit a bug in Boost.Python, but you tell me, simple c++ code - 
> and even simpler Python code - . Running bug.py segfaults for me without
> the line marked in bug.cc. I think it's because DerivedWrapper uses
> bases<Base>, but Base is exposed through BaseWrapper.
>  - http://dev.gentooexperimental.org/~peper/boost/bug.cc
>  - http://dev.gentooexperimental.org/~peper/boost/bug.py
I will try to explain, how I understand this, but of course I could be wrong.
bp::class_<DerivedWrapper, bp::bases<Base>, boost::noncopyable>
// Without this line the Python code segfaults
At run time, behind PyObject you have holds DerivedWrapper class instance.
During the first registration of "f", type of "this" pointer is
BaseWrapper. I could be wrong, but the library doesn't scan for
registered [pure] virtual functions and re-registers them again with
the right "this" pointer. So in your case: you call "f" with "this"
pointer type "DerivedWrapper", than library does casting for you from
DerivedWrapper to BaseWrapper and than it calls "f". Obviously this
cannot work, becase BaseWrapper and DerivedWrapper are not base and
You can test my explanation by changing DerivedWrapper implementation:
struct DerivedWrapper : Derived, BaseWrapper, bp::wrapper<Derived>
virtual int f() const
if (bp::override f = get_override("f"))
I think that in this case you don't have to register "f" twice.
C++ Python language binding
More information about the Cplusplus-sig