Goal: I'm trying to use python interactively in my c++ code using Boost::Python library. My goal is to change variables of some class which I have defined in c++ from the python interpreter.
The code is attached below.
Problem: I can load the library in python interface (i.e. load hello) and make an object out of it (obj = hello.World()). I even can access functions without variables ( obj.greet() ) but when I want to access functions with variables (obj.Set("Hello") ) I get memory access violation ("Access violation reading location 0xffffffffffffffff"). Even when that function is an empty function which just takes some string.
struct World
{
void set(string _msg) {}
string greet() { return msg; }
string msg;
};
typedef boost::shared_ptr<World> World_ptr;
BOOST_PYTHON_MODULE(hello)
{
bp::class_<World, World_ptr>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
int main(int argc, char **argv)
{
Py_Initialize();
bp::object main = bp::object(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__"))));
bp::object global(main.attr("__dict__"));
inithello();
// Bring up Python interpreter
Py_Main(argc, argv);
Py_Finalize();
return 0;
}
Actually this comes from Boost::Python tutorials with some modification to use Python interpreter http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html
Many thanks
--------------------- Edit ---------------------
I tested a lot and found out that the problem is only with string inputs. i.e. No problem with char* or int. Is there any problem with strings in boost python?