[C++-sig] Strange boost::python::object behaviour

Marek Denis marek at octogan.net
Wed Aug 4 15:09:35 CEST 2010


Hello everybody,

This is my first post on this mailing group so I wanted to say hello to 
all ml users ;)

I am encounetring a strange problem with boost::python::object in my module.

What I want to achieve is a module written in C++ with use of the 
boost::python, that can be imported to normal python application.
Python classes would do some additional parsing and put everything into 
the database.  However, in my C++ code I would like to launch a few 
methods from the instances of the classes that were defined in the 
Python code (different module).

 From the python point of view it should look like this:

#v+
m=cppmodule.cppclass()()
t=pythonmodule.pythonclass()

m.register_callback(t)
m.start()
#v-

and the 'm' instance should start it's work however invoking some 
methods of the 't' instance.

What I did was writing a method in my C++ code, something like:

#v+
void Manager::register_callback(boost::python::object&  obj)
{

    this->callback = obj;
    for(int i=0;i<5;++i)
    {
        std::cout << "Callback P()" << endl;
        callback.attr("p")();
    }
    return;

}
#v-

This is supposed to save an object to my C++ class so that I can use it 
later ( this->callback = obj; )
I did some tests, by  invoking some 'test' method of the Python object 
(method called 'p'), that just prints some string. It does work when I 
invoke register_callback method. However, later in code, when I invoke 
other method, say (for tests now):

#v+
void Manager::test()
{

     callback.attr("p")();

     return;
}
#v-

I get the "segmentation fault".
I should admit, that the callback variable is inside the global instance 
of the Manager object, none of the objects are deleted.
Where should I look for a solution, what do I wrong?

Thanks in advance!

-- 
pozdrawiam

Marek Denis



More information about the Cplusplus-sig mailing list