[C++-sig] boost::python segfault related to extract

Neal D. Becker ndbecker2 at verizon.net
Wed Oct 6 16:54:51 CEST 2004


I ran into a problem with code that assigns a slice of a vector of double to
a vector of complex.  The code will cause the following (this code is
shamelessly stolen):

    std::vector<value_type> temp (si.m_sliceLength);
    for (int i = 0; i < si.m_sliceLength; i++)
      {
 extract<value_type const&> x(new_value[i]);
 //  try if elem is an exact Data type
 if (x.check())
   {
     temp[i] = x();
   }
 else
   {
     //  try to convert elem to Data type
     extract<value_type> x(new_value[i]);
     if (x.check())
       {
  temp[i] = x(); <<<<<=== here
       }
     else
       {
  PyErr_SetString(PyExc_TypeError, 
    "Invalid sequence element");
  throw_error_already_set();
       }
   }
      }          
   

The segfault occurs at the indicated line.  This calls:

  template <class T>
  inline typename extract_rvalue<T>::result_type
  extract_rvalue<T>::operator()() const
  {
      return *(T*)(
          // Only do the stage2 conversion once
          m_data.stage1.convertible ==  m_data.storage.bytes
             ? m_data.storage.bytes
             : (rvalue_from_python_stage2)(m_source, m_data.stage1,
registered<T>::converters)
          );
  }

which calls:

BOOST_PYTHON_DECL void* rvalue_from_python_stage2(
[...]
    // If a construct function was registered (i.e. we found an
    // rvalue conversion), call it now.
    if (data.construct != 0)
        data.construct(source, &data); <<<<<<<<<<< here

which calls:


  // Given a target type and a SlotPolicy describing how to perform a
  // given conversion, registers from_python converters which use the
  // SlotPolicy to extract the type.
  template <class T, class SlotPolicy>
  struct slot_rvalue_from_python
  {
[...]

          // record successful construction
          data->convertible = storage; <<< here


which calls:
template <class T>
inline handle<T>::~handle()
{
    python::xdecref(m_p);  <<< here
}

with  *m_p
$7 = {ob_refcnt = 0, ob_type = 0x9114474}

Any ideas?
 




More information about the Cplusplus-sig mailing list