Boost logo

Boost :

From: David GD Wilson (firestar_at_[hidden])
Date: 2002-06-01 14:01:25


I am hoping someone has managed to get this problem working before and
can give me some pointers as to how to accomplish the task.

What I am trying to do is have a class in C++ that is mirrored in
python. This means that each instance of a class that is created will
have a python class created along with it and that python instance can
call into the c++ instance and vice versa.


CTest = c++ class
PTest = python class

CTest ca - ca references pa so it can call methods implemented in python
PTest pa - pa references ca so it can call methods implemented in c++

I have managed to create the python class from c++ and export the c++
class using boost.python however I am having great difficulty in making
the two classes reference each other - I get damaged memory blocks, this
suggests that I am doing something incorrectly!

Here is a snippet of the code I am using to make the python class
reference the c++ class:

// create argument for python class - the c++ instance pm
PyObject *pArgs = Py_BuildValue( "(O&)",
static_cast< CTest* >( pm ) );
if( !pArgs )
        return false;

// get an instance of the class PTest
pInstance = PyEval_CallObject( pClass, pArgs );
if( !pInstance )
        return false;
Py_DECREF( pArgs ); // program terminates here due to damaged memory

And the to_python function is:

PyObject* to_python( CTest *pTestIn )
    return python_extension_class_converters< CTest
>::smart_ptr_to_python( pTestIn );

PyObject* to_python( const CTest *pTestIn )
    return to_python( const_cast< CTest* >( pTestIn ) );

Boost list run by bdawes at, gregod at, cpdaniel at, john at