[C++-sig] Boost.Python C++ object reference in Python: unexpected behaviour.

Stefan Seefeld stefan at seefeld.name
Tue Jun 2 16:55:21 CEST 2015


On 02/06/15 10:36 AM, Christoff Kok wrote:
> Hi Stefan,
>
> Thank you very much. That makes sense and my tests prove it. The code
> runs as expected when I reserve enough space for the vector.
>
> I do not quite get it why it works in C++ and not python. I know too
> little about the C++ and python run-time.

What works in C++ and not in Python ? With the "reserve" calls the
Python script you provided runs fine (for me).

> I guess that the C++ run-time automatically updates objects holding
> references to its new address, whereas this is not the case in the
> Python run-time.
> Fixing that issue for me is way over my head at the moment.

But the problem really is in your C++ code, and has nothing to do with
the Python bindings. So you should start by clarifying your desired API,
then adjust the implementation. (Do you really want references to be
usable ? In that case, your vectors shouldn't store cars by-value. My
guess is you shouldn't be using references, unless the real types are
heavy objects, in which case you also want to prevent copy-construction,
and store heap-allocated objects.

Once the C++ API is settled, you can reconsider the appropriate Python
API for it.

>
> Thank you very much for spending the time to think about the problem
> and kudos to you for discovering the reason.

You are welcome.

    Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list