On 2/22/07, Roman Yakovenko <roman.yakovenko@gmail.com> wrote:
On 2/22/07, Gustavo Carneiro <gjcarneiro@gmail.com> wrote:
>   That wiki entry didn't apply anymore probably due to changing
> boost::python interfaces, but it provided very useful clues.  I managed to
> get it working 100% correctly using another hack.  Here's the diff of the
> manual changes I made to the wrapper:
>
> --- ns3.cpp     2007-02-21 23:46:44.000000000 +0000
> +++ ns3-working.cpp       2007-02-21 23:44:34.000000000 +0000
> @@ -9,19 +9,32 @@
>   namespace bp = boost::python;
>
>   struct EventImpl_wrapper : ns3::EventImpl, bp::wrapper< ns3::EventImpl > {
> +    ~EventImpl_wrapper() {
>  +        if (this->pyobj) {
> +            Py_DECREF(this->pyobj);
> +            this->pyobj = 0;
>  +        }
> +
>  +    }
>
>      EventImpl_wrapper()
>      : ns3::EventImpl()
>  -      , bp::wrapper< ns3::EventImpl >(){
> -        // null constructor
>  +      , bp::wrapper< ns3::EventImpl > (), pyobj(0) {
>
>      }
>
>      virtual void Notify(  ){
>  +        if (!this->pyobj) {
> +            this->pyobj =
> bp::detail::wrapper_base_::get_owner(*this);
>  +            Py_INCREF(this->pyobj);
> +        }
>           bp::override func_Notify = this->get_override( "Notify" );
>           func_Notify(  );
>      }
>
> +protected:
>  +    PyObject *pyobj;
> +
>   };
>
>  static void Schedule_b8544467c482930a621aca2e7ac87dca(
> std::auto_ptr< ::ns3::EventImpl > event ){
>

Hi Gustavo. I tested the code you post and it doesn't work for me :-(.
I also doesn't understand how it works for you. You should increment reference
on the object before you pass it "Schedule" member function, otherwise
you get same "Segmentation fault". Am I missing something?
I can avoid it by commenting the destructor code.

  You are right.  In my case it worked due to a bug in my code.  I forgot to make ~EventImpl() virtual, and so ~EventImpl_wrapper() was never called! :P

  I hate C++ :(

Also I added new functionality, which allows you to implement the patch:
http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/unittests/transfer_ownership_tester.py?view=markup
http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/unittests/data/transfer_ownership_to_be_exported.hpp?view=markup

  Thanks, it works perfectly! :-)

--
Gustavo J. A. M. Carneiro
"The universe is always one step beyond logic."