[C++-sig] Segfault when calling python method from C++
Pierre Barbier de Reuille
pierre.barbier at cirad.fr
Fri Jul 18 14:51:25 CEST 2003
I have an embedded python in a C++ program. I extend python using boost in order to interact with C++. Some of my methods makes use of C++ function-object. So I implemented this class to encapsulate Python methods :
*****************************************************
struct PyCenterComp
{
PyCenterComp( boost::python::object o );
PyCenterComp( const PyCenterComp& copy );
vertex_type operator()( Delaunay2D::cell_descriptor cell );
boost::python::object method;
};
PyCenterComp::PyCenterComp( object o )
: method( o )
{
}
PyCenterComp::PyCenterComp( const PyCenterComp& copy )
: method( copy.method )
{
}
vertex_type PyCenterComp::operator()( Delaunay2D::cell_descriptor cell )
{
std::cerr << "Call method ... " << endl;
boost::python::object o( method( cell_descriptor( cell ) ) );
std::cerr << "Called" << endl;
extract<vertex_type> v( o );
std::cerr << "Converted" << endl;
if( v.check() )
{
return v();
}
return vertex_type();
}
*******************************************
The Delaunay2D::cell_descriptor being implemented as void*, it's encapsulated in a cell_descriptor structure which is exported in Python.
In the python interpreter I created a function object with this code :
*******************************************
class ProdMean:
def __init__(self, walls):
self.walls = walls
def __call__(self,cell):
poly = self.walls.getCell(cell)
return self.mean(poly)
def mean(self, poly):
dx = 1
dy = 1
for i in poly:
dx = dx * i.x()
dy = dy * i.y()
dx = dx ** (1.0/len(poly))
dy = dy ** (1.0/len(poly))
return vertex_type(dx,dy)
*******************************************
Then I can use this class with :
p = ProdMean(walls)
l = [p(i) for i in walls]
walls is a C++ object (a graph) and the getCell method returns a polygon (a vector of vertex). This little python code works well. But now, if I try to call p from C++ using the PyCenterComp object I get a segmentation fault when calling the method (I got this output:
Call method ...
zsh: segmentation fault
)
If you want, I can give you the callstack when the crash occure, but it's quite huge (~50 elements ... ) so I'll do only if it's needed.
Thanks,
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
More information about the Cplusplus-sig
mailing list