Re: [pygccxml-development] Parameter passing, ownership semantics
Brought to you by:
mbaas,
roman_yakovenko
|
From: Gustavo C. <gjc...@gm...> - 2007-02-19 12:23:48
|
On 2/19/07, Roman Yakovenko <rom...@gm...> wrote:
>
> On 2/19/07, Gustavo Carneiro <gjc...@gm...> wrote:
> > Even with the name mangling problem, something was not right:
> >
> > gjc@nazgul:python$ python test-schedule.py
> > Traceback (most recent call last):
> > File "test-schedule.py ", line 11, in <module>
> > Simulator.Schedule_fbf096d706be19b392c1e3fbcd3bf544(t1,
> > ev)
> > Boost.Python.ArgumentError: Python argument types in
> >
> > Simulator.Schedule_fbf096d706be19b392c1e3fbcd3bf544(Time,
> > MyEvent)
> > did not match C++ signature:
> > Schedule_fbf096d706be19b392c1e3fbcd3bf544(ns3::Time
> > time, std::auto_ptr<ns3::EventImpl> event)
> >
> > Then I saw something I missed in the unit test, and added this code:
> >
> > EventImpl = mb.class_('EventImpl')
> > EventImpl.held_type = 'std::auto_ptr< %s >' % EventImpl.decl_string
> >
> > (EventImpl is the type of the argument that is subject to the
> ownership
> > transfer)
> >
> > But with the above changes the code doesn't compile (errors
> below). The
> > generated source contains:
> >
> > bp::class_< EventImpl_wrapper, std::auto_ptr< ::ns3::EventImpl >,
> > boost::noncopyable >( "EventImpl" )
> > .def( bp::init< >() )
> >
> > The error comes from the ".def( bp::init< >() )" line. Manually
> removing
> > the "std::auto_ptr< ::ns3::EventImpl >" part from the first line solves
> the
> > compilation error.
> >
> > Any hints?
>
> Yes:
> 1. change "std::auto_ptr< ::ns3::EventImpl >" to
> "std::auto_ptr< EventImpl_wrapper >" and add next line to your code:
>
> ei = mb.class _( 'EventImpl' )
> ei.add_registration_code(
> "boost::python::implicitly_convertible< std::auto_ptr<
> EventImpl_wrapper >, std::auto_ptr< ::ns3::EventImpl > >();"
> , False )
>
> This should help.
Thanks, Roman! I helped.
2. If not, please create small standalone test case, which reproduce the
> problem.
> I will take a look on it.
Unfortunately after all this effort I am still experiencing similar memory
errors as before. I don't understand why because, as far as I can tell,
Py++ generated the boost code according to the boost FAQ advice. In this
code, if I uncomment the 'del ev' line I get a memory error and segfault:
from ns3 import *
class MyEvent(EventImpl):
def Notify(self):
print "Notify!"
ev = MyEvent()
Simulator.Schedule(Seconds(1), ev) # takes ownership of ev
#del ev
Simulator.Run() # calls ev.Notify and then destroys it; memory error here
Here's what valgrind reports:
==9790== Invalid read of size 4
==9790== at 0x4B6E960: (within
/usr/lib/libboost_python-gcc-mt-1_33_1.so.1.33.1)
==9790== by 0x8097F18: (within /usr/bin/python2.5)
==9790== by 0x8062CED: (within /usr/bin/python2.5)
==9790== by 0x4ABE032: boost::python::api::object_base::~object_base()
(object_core.hpp:436)
==9790== by 0x4ABE080: boost::python::api::object::~object()
(object_core.hpp:294)
==9790== by 0x4ABE176: boost::python::override::~override() (override.hpp
:88)
==9790== by 0x4ADFC4F: EventImpl_wrapper::Notify() (ns3.cpp:32)
==9790== by 0x4AF9CF5: ns3::EventImpl::Invoke() (event-impl.cc:39)
==9790== by 0x4B005DE: ns3::SimulatorPrivate::ProcessOneEvent() (
simulator.cc:138)
==9790== by 0x4B0061E: ns3::SimulatorPrivate::Run() (simulator.cc:168)
==9790== Address 0x497D838 is 0 bytes inside a block of size 12 free'd
==9790== at 0x4020F9A: free (vg_replace_malloc.c:233)
==9790== by 0x4B6E766:
boost::python::instance_holder::deallocate(_object*, void*) (in
/usr/lib/libboost_python-gcc-mt-1_33_1.so.1.33.1)
==9790== by 0x4B6E97F: (within
/usr/lib/libboost_python-gcc-mt-1_33_1.so.1.33.1)
==9790== by 0x8097F18: (within /usr/bin/python2.5)
==9790== by 0x8086418: PyDict_DelItem (in /usr/bin/python2.5)
==9790== by 0x8061656: PyObject_DelItem (in /usr/bin/python2.5)
==9790== by 0x80C27F9: PyEval_EvalFrameEx (in /usr/bin/python2.5)
==9790== by 0x80C60F4: PyEval_EvalCodeEx (in /usr/bin/python2.5)
==9790== by 0x80C6166: PyEval_EvalCode (in /usr/bin/python2.5)
==9790== by 0x80E590B: PyRun_FileExFlags (in /usr/bin/python2.5)
I think I'll try to make a small example to reproduce this...
--
Gustavo J. A. M. Carneiro
"The universe is always one step beyond logic."
|