[C++-sig] copy boost::python::object crashes

Jim Bosch talljimbo at gmail.com
Sat Jan 23 22:38:47 CET 2010


On Sat, 2010-01-23 at 15:59 +0000, sergun wrote:
> I have a problem with storing reference to Python object inside C++.
> 
> The following Boost.Python wrapping class:
> 
> 
> 
> #include <boost/python.hpp>
> 
> using namespace boost;
> using namespace boost::python;
> 
> namespace cmodule
> {
> 
> object y;
> 
> void setY(object y1) { y = y1; }
> 
> }
> 
> using namespace cmodule;
> 
> BOOST_PYTHON_MODULE(cmodule)
> {
> 	def("setY",setY);
> }
> 
> 
> 
> 
> Crashes with “Segmentation fault” on so simple python fragment.
> 

This might be a static initialization problem: you declare "y", but it's
not clear from your code fragment whether it gets initialized.  I would
have expected that to result in a linker error, but the way Python does
its dynamic loading might have suppressed that.

It's probably a good idea to avoid global variables in source files
anyhow.  Wrapping it in a function:

object & getY() {
    static object y;
    return y;
}

void setY(object y1) { getY() = y1; }

might eliminate your segfault.


Jim Bosch



More information about the Cplusplus-sig mailing list