Skip to main content
GameDev.net gamedev.net
🔒 Locked

strings in boost python

Started by crazedfool Jun 30, 2006 at 4:26 PM 5 replies 1.2k views
Original Post
crazedfool
crazedfool
I'm using boost to export an interface to python, however, I've run into some problems with sending strings from python to C++. Whenever I attempt to do so, the program crashes with an access violation. I've narrowed it down to this code which causes the same error.

void print_it(string data)
{
	cout << data;
}

BOOST_PYTHON_MODULE(c1)
{
	def("print_it", &print_it);
}

Scripts::Scripts(string module)
{
	Py_Initialize();

	initc1();
	try
	{
		object name = str("run");
		_MainModule = object( handle<>( borrowed( PyImport_Import(name.ptr()) ) ) );
		object d = _MainModule.attr("__dict__");
		_Start = d["run"];
	}
	catch(error_already_set)
	{
		PyErr_Print();
		throw;
	}
	_Start();
}

The function run in module run imports c1 and then calls print_it. There is a breakpoint on the first line of print_it but it is never reached. Am I missing some integral step in getting strings to work in boost python? I've searched on the internet but can't find anything pertinent, and the examples on the boost python website seem to use strings with no additional setup. Thanks in advance for any help.
SiCrane
SiCrane
Using this as the python file:
import c1;def run():  c1.print_it("string");

I took your code and ran with no problems (aside from the modifications needed to get things to compile in main() rather than in your class constructor). Possiblities: It could be a problem somewhere else in your code such as a buffer overrun that's causing the issue. Also, did you remember to enable RTTI for your project?
crazedfool
crazedfool
Hmm, alright, I created a whole new project with absolutely nothing else, and I'm still getting the access violation

c2.cpp
#include <iostream>#include <string>#include <boost/python.hpp>using namespace std;using namespace boost::python;void print_something(){	cout << " I eat brains";}void print_it(string data){	cout << data;}BOOST_PYTHON_MODULE(c1){	def("print_it", &print_it);	def("print_something", &print_something);}int main(int argc, char* argv[]){	try	{		Py_Initialize();		initc1();		object name = str("run");		object _MainModule = object( handle<>( borrowed( PyImport_Import(name.ptr()) ) ) );		object d = _MainModule.attr("__dict__");		object _Start = d["run"];		_Start();	}	catch(error_already_set)	{		PyErr_Print();		throw;	}	return 0;}


run.py
import c1def run():    c1.print_something()    c1.print_it("stuff")


This causes an access violation on the line calling start. in c2.cpp. I've made sure the right module is being called and that rtti is on. Are there any other build flags I could be missing? Could my boost build have gotten corrupted? Is it possible that the built in string conversion functions got deleted somehow leading to an access violation? Any guidance would be great.

Edit: Added print_something to show that functions without a string argument work. I eat brains is printed to the console before the access violation, but the breakpoint in print_it is still never reached.
Edit2: I'm using Visual C++ Express and I just updated to whichever the newest version of boost is today.

[Edited by - crazedfool on June 30, 2006 5:07:49 PM]
crazedfool
crazedfool
The interface is exposed only while my program is running, I'm embedding python not writing an independent extension for it. So I can't just test the python file in a normal python.exe. Although that would be nice.
mikeman
mikeman
Ah ok. Anyway, I tested you project and it works fine. Are you sure you have built both your project and the boost_python.lib you link to with the same configurations? Just for testing, I built the project in debug mode and link it to a release 'boost_python.lib' and got exactly what you're describing: Access violation at 0xcccc0066 right after "i eat brains" is printed(btw, your debug logging is scary[smile]).
crazedfool
crazedfool
Haha, thanks man, you're my new hero, that's exactly what I'm doing, linking to a release build of boost python. Rate up for you, I just didn't stop to think that that was a bad idea.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.