No problem!

On 12/22/2012 5:08 PM, simon zhang wrote:
You are right.Thank you.

2012/12/23 Jaedyn K. Draper <jaedyn.cpp...@jaedyn.co <mailto:jaedyn.cpp...@jaedyn.co>>

    If you pass it to a function like that, it passes it by value even
    if it's a pointer. So it's calling the copy constructor (which you
    haven't defined so it isn't printing "A start"), rather than the
    regular constructor, and then destructing it.

    To pass an actual pointer rather than sending it by value, you
    need to do it like this:

    |
    B(){aa=newA;}~B(){deleteaa;}voidrun(object ct){_obj=ct();//creat a
    python object


             _obj.attr("fun1")(ptr(aa));   //call a function named "fun1" with 
a pointer arg
             _obj.attr("fun2")(ptr(aa));   //call a function named "fun2" with 
a pointer arg
         }
         A*aa;
         object _obj;|


    Note that both "aa" objects are wrapped by a call to "ptr()"

    Just be careful with the python when doing this, as it can cause a
    crash if python tries to access that object after its C++ lifetime
    has ended. So always make sure the python object's lifetime ends
    before the C++ object's does.


    On 12/22/2012 4:45 PM, simon zhang wrote:
    when I call python's function with pointer as an argument in
    boost.python, there are some troubles in destructor. The
    following is a sample code

    |#include  <boost/python.hpp>
    #include  <boost/python/module.hpp>
    #include  <boost/python/def.hpp>
    #include  <string>
    #include  <iostream>
    using  namespace  boost::python;

    class  A{
    public:
         A()  {
             std::cout<<  "A start"<<std::endl;
         }
         ~A()  {std::cout<<  "A end"  <<std::endl;}
    }
    class  B{
    public:
         B()  {  aa=new  A

    ;  }
~B() { delete
    aa;  }
         void  run(object ct)  {
             _obj=ct();               //creat a python object
             _obj.attr("fun1")(aa);   //call a function named "fun1" with a 
pointer arg
             _obj.attr("fun2")(aa);   //call a function named "fun2" with a 
pointer arg
         }
         A*aa;
         object _obj;
    }

    BOOST_PYTHON_MODULE(ctopy)
    {
         class_<A>  ("A",init<>())
         ;
         class_<b>  ("B",init<>())
         .def("run",&B::run)
         ;
    }|

    python:
    |import  ctopy
    class  tc:
         def fun1(self,tt):
             print"fun1"
         def fun2(self,tt):
             print"fun2"
    bb=ctopy.D()
    bb.run(tc)|
    this result:
    |A start
    fun1
    A end
    fun2
    A end
    A end|


      note:

    The "A end" has been printed three.I try it in "valgrind",there
    are some errors.I just want to run the destructor once.How to do?




    _______________________________________________
    Cplusplus-sig mailing list
    Cplusplus-sig@python.org  <mailto:Cplusplus-sig@python.org>
    http://mail.python.org/mailman/listinfo/cplusplus-sig


    _______________________________________________
    Cplusplus-sig mailing list
    Cplusplus-sig@python.org <mailto:Cplusplus-sig@python.org>
    http://mail.python.org/mailman/listinfo/cplusplus-sig




_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to