tags:

views:

21

answers:

0

I am wrapping a C++-based data storage library for python using boost.

The library is like this:

class Container
 ...
 Piece* get(int index)
 ...
 ...
class Piece
 ...

Each Container (object) is composed of several Pieces. When Container.get is called it will return a pointer to Piece inside Container instead of a full copy.

The problem is, if I use boost.python's "return pointee value" policy, both Container and Piece object, whose contents overlap in memory, will be freed in python's garbage collection. This will cause segment fault.

If I use boost.python's "return pointer address" policy, I can't access Piece inside Container for ctypes in python does support customized C++ objects.

How can I smoothly read Pieces inside Container in python?

NOTE: I finally found out it is exactly what return_internal_reference policy from boost.python is supposed to do.