[C++-sig] bug? : binding a method returning a possibly NULL polymorphic pointer

David Abrahams dave at boost-consulting.com
Wed Apr 16 16:36:14 CEST 2003


"Nicolas Lelong" <nico_ml at mgdesign.org> writes:

> Hi,
>
> I have a method that returns a shared_ptr<Base> object, where Base
> is a polymorphic class, and the shared_ptr can reference no object.
>
> I found out that when this method effectively returns a
>shared_ptr-wrapped NULL, I get an exception that tells me that typeid
>can't be applied on NULL - or that RTTI can't be extracted from a
>NULL object (I did not note the exact message provided by VC7). This
>error comes from 'get_derived_class_object(mpl::true_, [...])' in
>make_instance_ptr.hpp.
>
> The original code is :
>
> template <class U>
> static inline PyTypeObject* get_derived_class_object(mpl::true_, U const volatile* x)
> {
>   converter::registration const* r = converter::registry::query(type_info(typeid(*x)));
>   return r ? r->m_class_object : 0;
> }
>
> I'd like to suggest the following implementation that does not
> execute typeid on a dereferenced NULL pointer, and lets the Python
> script deal with a None value :
>
> template <class U>
> static inline PyTypeObject* get_derived_class_object(mpl::true_, U const volatile* x)
> {
>   converter::registration const* r = x ? converter::registry::query(type_info(typeid(*x))) : 0;
>   return r ? r->m_class_object : 0;
> }
>
> I had a quick look and this modification seems to be sufficient to
> correct the problem.  Cheers,

That's not quite the right fix because  shared_ptr<A>() gets
converted to a Python A object with no C++ data, whereas it should get
converted to None.  I made the right fix in CVS and merged it to
RC_1_30_0 in case we do a 1.30.1 release.

Thanks for the report,
Dave

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





More information about the Cplusplus-sig mailing list