[C++-sig] Re: Virtual member functions cause exception in get_derived_class_object

David Rostowsky drostowsky at radioframenetworks.com
Thu Apr 1 22:02:30 CEST 2004


Attached are the files in question. Thanks for the direction. Im a newbie to
these lists. In my original post I was trying to post the minimal that
failed, so hence I wanted to give everything in the follow-up. Ill try to
find a happier middle ground for future posts.




"David Abrahams" <dave at boost-consulting.com> wrote in message
news:uwu4zvpit.fsf at boost-consulting.com...
>
> Hi David,
>
> First of all, please bring Boost.Python questions to the C++-sig:
> http://www.boost.org/more/mailing_lists.htm#cplussig
>
> "David Rostowsky" <davros1 at covad.net> writes:
>
> > I have a couple of really simple classes:
> >
> > class MyBase
> > {
> > public:
> >   int x;
> >   virtual int foo(int new_x) { }; // our derived classes may or may
> > not override this function. Coders choice.
> > };
> >
> > class MyDerived: public MyBase
> > {
> > public:
> >   int y;
> >   MyDerived() { y = 0; };
> >   int Get(void) { return y; };
> >   void Set(int val) { y = val; };
> > };
> >
> > My main goal in life is to create an instance of MyDerived class in
> > C++:
> > MyDerived my_derived = new MyDerived();
> >
> > and then pass the pointer to a Python script where the Python script
> > can call any of the public member functions. Yes, I am careful to keep
> > the pointer around in the C++ code while the Python script uses it.
> > So I expose the class (this may or may not be the source of my
> > problems, need help):
> >
> > BOOST_PYTHON_MODULE(test)
> > {
> >     using namespace boost::python;
> >
> >     class_<MyDerived>("MyDerived")
> >         .def("Get", &MyDerived::Get)
> >         .def("Set", &MyDerived::Set)
> >         ;
> > } /* test*/
> >
> > My impasse is that the virtual function seems to really give Boost
> > hell when Im trying to pass the pointer to the class over to the
> > python script. Heres the code snippet for that.
>
> Please post a complete, minimal, reproducible example.
>
> > Its the conversion thats just KILLING me.
>
> Chill, dude.  You're still alive.  At least someone's sending email
> on your behalf ;-)
>
> > If I comment out the virtual function, everything works perfectly! I
> > can pass the pointer over to my script, it calls the public member
> > functions, and Im happy. HOWEVER, if I put the virtual function back
> > in, the following line of code
>
> Well you've posted several lines there; can you be specific about
> which one you're having problems with?
>
> > just EXPLODES!
>
> What does "EXPLODES" mean?  Do you get a compilation error, a runtime
> error, or something else?
>
> > // It crashes and burns in make_ptr_instance.cpp, function
> > get_derived_class_object()
> > boost::python::object my_arg(boost::ref(my_derived));
> >
> > // pass the pointer over to the script now.
> > int rval = callPython("myTestFunc", "Test String argument from c++",
> > my_arg);
>
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
>


begin 666 test_script.py
M=')Y. at T*(" @("!I;7!O<G0 at 9W)U;7-H#0H@(" @('!R:6YT(")0>71H;VXZ
M($EM<&]R="!354-#145$140A(2$B#0IE>&-E<'0Z#0H@(" @('!R:6YT(")0
M>71H;VXZ($EM<&]R="!&04E,140N+BXB#0H-"B -"B,C(R,C(R,C(R,C(R,C
M(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C
M(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,-"B, at 5&5S="!M;V1U;&4-"B,C
M(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C
M(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,-"F1E9B!M
M>51E<W1&=6YC*&US9RP at 8W!P3V)J*3H-"@T*(" @(" C(%1E<W0@=&AE(&]B
M:F5C="!E>'!O<V5D(&9R;VT at 8RLK("A087-S960 at 87, at 82!F;W)M86P@<&%R
M86UE=&5R*0T*(" @(" C(%-H;W<@:61E;G1I9GD@;V8 at 8W!P3V)J#0H@(" @
M('!R:6YT(")0>71H;VXZ($ED96YT:71Y(&]F(&5X<&]S960 at 8W!P3V)J.B B
M+ T*(" @("!C<'!/8FHN<')I;G10='(H*0T*(" @("!P<FEN= T*#0H@(" @
M(", at 4VAO=R!S=&%T92!O9B!C<'!/8FH-"B @(" @<')I;G0@(E!Y=&AO;CH@
M8W!P3V)J+D=E="@I(&]N(&5N=')Y.B(L(&-P<$]B:BY'970H*0T*#0H@(" @
M(", at 0VAA;F=E('-T871E(&]F(&]B:F5C= T*(" @("!C<'!/8FHN4V5T*#DI
M#0H-"B @(" @(R!3:&]W('-T871E(&]F(&]B:F5C= T*(" @("!P<FEN=" B
M4'ET:&]N.B!C<'!/8FHN1V5T*"D@;VX at 97AI=" Z(BP at 8W!P3V)J+D=E="@I
6#0H-"B @(" @<F5T=7)N(#DY.2 -"@``
`
end

begin 666 code2.cpp
M#0HO+R!';&]B86P@;6]D=6QE('!O:6YT97(-"F)O;W-T.CIP>71H;VXZ.FAA
M;F1L93P^(&UY7VUO9'5L93L-"@T*8VQA<W, at 0F%S93$-"GL-"G!U8FQI8SH-
M"@EI;G0@>CL-"@E"87-E,2 at I('L@>CT@,3L@?3L-"@EV:7)T=6%L('9O:60@
M3VY+97E5<"AU;G-I9VYE9"!S:&]R="!V:7)T=6%L7VME>2D@>R!].PT*#0I]
M.PT*#0IC;&%S<R!497-T0R Z('!U8FQI8R!"87-E,0T*>PT*<')O=&5C=&5D
M. at T*"6EN="!X.PT*#0IP=6)L:6,Z#0H-"@E497-T0R at I('L@>" ](# [('T[
M#0H)=F]I9"!3970H:6YT('9A;"D@>R!X(#T@=F%L.R!].PT*"6EN="!'970H
M=F]I9"D@>R!R971U<FX@>#L@?3L-"B @("!V;VED('!R:6YT4'1R*"D@>R!P
M<FEN=&8H(D,K*R @(#H at 4'1R/3!X)7 B+"!T:&ES*3L@?0T*?3L-"@T*+R\@
M4'ET:&]N('=R87!P97(@9F]R($UY0VQA<W,-"D)/3U-47U!95$A/3E]-3T15
M3$4H9W)U;7-H*0T*>PT*(" @('5S:6YG(&YA;65S<&%C92!B;V]S=#HZ<'ET
M:&]N.PT*#0H@("!O8FIE8W0@=&5S=$-L87-S(#T at 8VQA<W-?/%1E<W1#+"!B
M;V]S=#HZ<VAA<F5D7W!T<CQ497-T0SX@/B at -"B @(")497-T0R(L(&EN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&EM<&]R=%]M;V1U;&4H
M8V]N<W1?8V%S=#QC:&%R*CXH<V-R:7!T3F%M92YC7W-T<B at I*2D[#0H-"B @
M("!R971U<FX at 4W5C8V5S<SL-"GT-"@T*:6YT(&UA:6XH*0T*>PT*:6YT(')V
M86P[#0H-"E1E<W1#("H@=&5S=" ](&YE=R!497-T0R at I.PT*8F]O<W0Z.G-H
M87)E9%]P='(\5&5S=$,^('1E<W0Q<'1R*'1E<W0I.PT*#0H@(" @:6YT(')E
M='8@/2!I;FET4'ET:&]N*")T97-T7W-C<FEP="(I.PT*(" @(&EF*"!R971V
M(#T](%-U8V-E<W,@*0T*(" @('L-"@T*"2\J(%1H:7,@:7,@=VAE<F4 at 22!G
M970@=&AE(')U;BUT:6UE(&5X8V5P=&EO;B!I9B!)(&AA=F4@=&AE('9I<G1U
M86P@;65M8F5R(&9U;F-T:6]N(&EN($)A<V4Q+B -"@DJ*B!#;VUM96YT(&]U
M="!T:&4@=FER='5A;"!M96UB97(@9G5N8W1I;VXL(&%N9"!E=F5R>6]N92!I
M<R!H87!P>2$@#0H)*B\@(" @(" @( T*(" @(" @("!O8FIE8W0 at 87)G,BAT
M97-T,7!T<BD[#0H-"B @(" @(" @:6YT(')E='8@/2!C86QL4'ET:&]N*")M
M>51E<W1&=6YC(BP@(E1E<W0 at 4W1R:6YG(&%R9W5M96YT(&9R;VT at 8RLK(BP@
M87)G,BD[#0H-"B @("!]#0H-"B @("!R971U<FX@,#L-"@T*?2 O*B!M86EN
%("HO#0H`
`
end






More information about the Cplusplus-sig mailing list