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

Christoff Kok christoff.kok at ex-mente.co.za
Wed Jun 3 09:43:44 CEST 2015


Thank you again Stefan,

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

I tested the code in a C++ console application, using the same example as
in the Python example I posted.
I meant, that, without the "reserve" calls, the C++ console application
worked as I expected, and Python didn't. Python works as I expected with
the 'reserve' calls.

> 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.

The types I am using are big objects and performance is a concern.
C++11's 'move' semantics make storing large objects by value in a vector
much more viable and performant. (Very little overhead.)
I liked this approach of using containers of objects by value everywhere
(well, except where polymorphism is needed).
I am sure that using heap-allocated objects will still be faster however.
Even though moving has little overhead, it's still overhead heap-allocated
objects doesn't have to deal with.

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

I tested the use of heap-allocated objects and it works.
I am going to change my code to rather store heap allocated objects for all
my complex types (to keep it consistent for maintenance and simplicity's
sake).

Thank you for all your assistance, I greatly appreciate it. You saved me a
lot of time.

Regards,
Christoff

On 2 June 2015 at 16:55, Stefan Seefeld <stefan at seefeld.name> wrote:

> 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...
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Christoff Kok
Software Engineer
Ex Mente

http://www.ex-mente.co.za
christoff.kok at ex-mente.co.za
PO Box 10214
Centurion
0046
South Africa
tel: +27 12 743 6993
tel: +27 12 654 8198
fax: +27 85 150 1341
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20150603/3b1eeb85/attachment.html>


More information about the Cplusplus-sig mailing list