[C++-sig] wrapping reference to c++ object

harold fellermann harold at imb-jena.de
Mon Mar 17 18:13:54 CET 2003


hi.



>> As you might know in the meanwhile, I want to insert references (not
>> instances)
>> of C++ objects into a boost::python::list. I thought that calling
>>
>> CPPObject cpp_obj;
>> list.insert(0,handle<>(borrowed(cpp_obj)))
> First, please do what Ralf said and read the fine tutorial
> front-to-back beforte proceeding, or trying anything I've said below.
> You need to get a better sense of how the library works in order to
> understand any advice we can give here.

I did that, you were right. I also did some reading about extending
and embedding python. It's getting clearer
now, but there is still one questian unsolved (see below).

> One way, but not neccessarily a safe one, to get a Python object into
> your list which wraps a reference to a C++ object, is
>
>      list.insert(0, boost::ref(cpp_obj));
>
> I say "not neccessarily safe" because there is nothing here to help
> manage the lifetime of the C++ object properly; if it is destroyed
> before the last Python reference you could crash the program with
> Python code.

This works. I use with_custodian_and_ward to bind the lifetime of
cpp_obj to the object that inserts it into its list of references.
There encountert no lifetime problem when using the classes as
python extensions.

However, trying to use my classes with embedded python still troubles 
me.
The thing I don't understand is the following: when I insert a cpp_obj
in python, I deal with a python::object because of the wrapper classes,
which works fine. But when I use the same (unwrapped) classes for 
embedded
python, any attempt to insert ref(cpp_obj) into my python::list still 
dumps.
What is the reason therefore? Is there a concept I haven't understood 
yet?


Thank you for the patience!

- harold -

--
"2x2 = grün"
-- Heinz von Foerster





More information about the Cplusplus-sig mailing list