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

Nicolas Lelong nico_ml at mgdesign.org
Mon Apr 14 10:52:53 CEST 2003


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,

Nicolas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030414/da4d1ba5/attachment.htm>


More information about the Cplusplus-sig mailing list