[C++-sig] Extracting and returning a c++ object from python and garbage collection

William Ladwig wladwig at wdtinc.com
Tue Aug 19 22:13:08 CEST 2008


My actual code is a little different than what is posted here, but the basic idea is that we use many different input formats that are similar (all represent "2D" arrays of data), but need to be converted to a common "2D" array format before being processed.  For this, I am using an abstract base class where one of the virtual functions handles the conversion and returns an auto_ptr to the required input type.  This is easy to do in C++, but if I want to override that virtual function in python, I'm not quite sure how to return an auto_ptr to a C++ object.  (I'm trying to allow for the development of the input portion of the application to be done in either c++ or python).

When I initially tried to do this, I wrapped the GenericInput class as:

struct GenericInputWrapper : GenericInput, wrapper<GenericInput>
{
        GenericInputWrapper()
        :       GenericInput(), wrapper<GenericInput>() {}

        std::auto_ptr<ReqInput> void convToReqFormat()
        {
                std::auto_ptr<ReqInput> r;
                r.reset(this->get_override("convToReqFormat"));

                return r;
        }

};

However, this threw the "dangling reference" error.  So, I figured I needed to extract the python object first, and then convert it to an auto_ptr.

-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Roman Yakovenko
Sent: Tuesday, August 19, 2008 2:40 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Extracting and returning a c++ object from python and garbage collection

On Tue, Aug 19, 2008 at 10:16 PM, William Ladwig <wladwig at wdtinc.com> wrote:
>
>
> I'm having a problem overriding a pure C++ virtual function in python which returns an auto_ptr to a C++ type (which has been exposed to python).  It looks like I am extracting the class correctly in my base class wrapper, but when the function returns to python, it throws a segmentation fault if I try to access the returned object.  I suspect I have a garbage collection problem, but I'm not sure what I'm doing wrong.  How do I convert an object from python to C++ and return the C++ object without it being garbage collected?  Here is a sample to illustrate the problem:
>
> C++ code:
> ...
>        // ********** Here is where my problem is *********************
>        std::auto_ptr<ReqInput> void convToReqFormat()
>        {
>                // This will return a python object of the exposed ReqInput type
>                override func = this->get_override("convToReqFormat");
>                object py_obj = func();
>
>                ReqInput& req_in = extract<ReqInput&>(py_obj);
>                std::auto_ptr<ReqInput> r;
>                r.reset(&req_in);

At some point you have same pointer, managed by 2 objects.

Also why do you need to write such code?

I attach the code, generated by Py++. I am pretty sure it will work as-is.


--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list