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

William Ladwig wladwig at wdtinc.com
Wed Aug 20 21:11:25 CEST 2008


I am using an older version of gcc (3.4.6) on Redhat Linux.  My python interpreter is v2.5.2

Upon doing some digging using google, I found this thread where the poster is doing something very similar to what I am trying to do:

http://mail.python.org/pipermail/c++-sig/2005-May/008867.html

The compiling errors are due to the way the auto_ptr is defined in the STL.  I don't think the suggested fix in the thread compiled for me either due to "ambiguous" overloaded function errors.  So, I ended up using (before I posted my thread yesterday):

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

This code will compile, but if I try to use the returned object, it crashes with a segmentation fault, which I suspect is because the python exposed "c++" object returned by this->get_override() is being garbage collected when the wrapper function returns.  One solution that fixed the problem for me was to extract to the ReqInput object, then create a new ReqInput object which copies the extracted object, and returns an auto_ptr to the new object.

std::auto_ptr<ReqInput> void convToReqFormat()
{
        object py_input = this->get_override("convToReqInput")();
        ReqInput& r_in = extract<ReqInput&>(py_arr);
        std::auto_ptr<ReqInput> r(new ReqInput(r_in));
        return r;
}

This is horribly inefficient (considering the object contained in my real code contains a 6000x6000 array), but doesn't crash on me.  I never tried extracting an auto_ptr directly however, which is probably what I should have been doing all along, since that is the object that the virtual function is returning.  I'll give it a try and see how it works out.  Thanks!




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

On Wed, Aug 20, 2008 at 1:03 AM, William Ladwig <wladwig at wdtinc.com> wrote:
> I looked at the Py++ code and tried it out (it's similar to how I tried it originally) and it also throws a "dangling pointer" exception from the interpreter.

Interesting, I've got other errors.

Using Visual Studio 2003, I got warning during compilation and
TypeError during runtime, which says, that Boost.Python is not able to
construct rvalue, std::auto_ptr< your class >, from the Python object.

Using gcc 4.2.3, on Ubuntu the generated code didn't compiled and when
I fixed it, than everything was fine and worked as expected
I attached source code, "fixed" generated code and small tester.

What is your environment?

Any way, it seems that I found solution. The only change was to
extract std::auto_ptr<> as is, by value, from Python object.

Let me know, if this really solves your problem, I will fix Py++.

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



More information about the Cplusplus-sig mailing list