[C++-sig] custom smart pointers

David Abrahams dave at boost-consulting.com
Wed Sep 13 14:24:45 CEST 2006


"Roman Yakovenko" <roman.yakovenko at gmail.com> writes:

> On 9/12/06, David Abrahams <dave at boost-consulting.com> wrote:
>> I made your example work.  I had to add an operator* to your smart
>> pointer, although I think that uncovers a bug in Boost.Python: we
>> should be using get_pointer where it needed the operator*, instead.
>>
>> I don't know if this meets your needs or not, but the result is
>> enclosed.
>
> Thank you for your time and help.
>
> Unfortunately it did not help :-(.
> I reworked the example, base on your comments.
>
> I had to add next conversion:
> bp::implicitly_convertible< smart_ptr_t< base_wrapper_t >,
> smart_ptr_t< base_i > >();
>
> There are still 2 problems: I am not able to call functions that take
> reference and const reference to smart_ptr_t< base > instance.

FWIW, reference and const reference are totally separate cases.  const
reference is essentially the same as pass-by-value; it only requires
an rvalue conversion.  Mutable references require an lvalue conversion.

> int ref_get_value( smart_ptr_t<base_i>& a ){
>    return a->get_value();
> }
>
> I am unable to call this function, I've got ArgumentError exception

Naturally; there is no instance of smart_ptr_t<base_i> anywhere in the
Python object for the reference to bind to.   The rules are the same
as in C++: 

   int f(smart_ptr_t<base_i>& x) { return 0; }
   smart_ptr_t<base_wrapper_t> y;
   int z = f(y);                // fails to compile

> int const_ref_get_value( const smart_ptr_t<base_i>& a ){
>    return a->get_value();
> }
>
> I am unable to call this function, I've got "segmentation fault" on Linux.

Where does the segmentation fault occur?  Is 'a' a valid pointer
within const_ref_get_value?

> I attached test.py file that checks all the functions.

Try building a minimal example that does nothing more than to
reproduce your segmentation fault.  Then we'll see if the problem is
in Boost.Python or in your code.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list