[C++-sig] with_custodian_and_ward problem

Roman Yakovenko roman.yakovenko at gmail.com
Sun Jun 10 09:50:03 CEST 2007


On 6/9/07, Hans Meine <hans_meine at gmx.net> wrote:
> Hi Roman,
>
> thanks for looking at my problem.
>
> On Samstag 09 Juni 2007, Roman Yakovenko wrote:
> > > I created a testcase (attached, too):
> > >
> > > This is my class which internally stores a reference to an object given
> > > to the
> > >
> > > constructor:
> > > > struct T
> > > > {
> > > >     T(int &foo) : foo_(foo) {}
> > > >     int &foo_;
> > > > };
> >
> > The constructor takes immutable type( int ) by reference. You cannot
> > directly expose this constructor.
>
> Argh.  OK.  That's an artifact caused be /me trying to strip everything down
> as much as possible (in the original, complext classes and rvalue converters
> were involved).  Just imagine I had used my own class.
>
> > > > int someInt;
> > > >
> > > > static T *createT()
> > > > {
> > > >     return new T(someInt);
> > > > }
> > >
> > > [...]
> > > >     class_<T, boost::noncopyable>("T", no_init)
> > > >         .def("__init__", make_constructor(
> > > >                  &createT,
> > > >                  with_custodian_and_ward_postcall<0, 1>()));
> > >
> > > How can I properly tell BPL to make sure that the object passed to the
> > > constructor must not be deleted before the "T" instance?
> >
> > In the example you gave you didn't pass any argument to "createT" function.
>
> Oh, right.  Again, I messed up the testcase.  I hope I did better this time,
> see attachment.

I guess you should put call policies on "def" and not on
"make_constructor". I didn't test this:

.def("__init__"
       , make_constructor( &createT )
       , with_custodian_and_ward_postcall<0, 1>() );


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list