[C++-sig] baffling memory leak with libboost 1.29.0

F. Oliver Gathmann gathmann at cenix-bioscience.com
Wed Aug 7 09:59:16 CEST 2002


Ralf W. Grosse-Kunstleve wrote:

>--- "F. Oliver Gathmann" <gathmann at cenix-bioscience.com> wrote:
>>   PyObject * get_as_pyobject(const std::string& country)
>>   {
>>     std::auto_ptr<hello> p(new hello(country));
>>     return BOOST_PYTHON_CONVERSION::to_python(p);
>>   }
>> 
>> }
>> 
>> ------------------------------------------------------------------------
>> 
>> Now, while both calls to get_as_pointer() and get_as_pyobject() will
>> return a "hello" instance as expected, get_as_pyobject() leaks memory
>> while get_as_pointer() doesn't. This is particularly surprizing as I
>> thought that if I return a smart pointer p directly (as this is done
>> in get_as_pointer()), the library just calls to_python(p) internally,
>> which is exactly what I am doing in get_as_pyobject(). Can anybody
>> explain to me what I am doing wrong here?
>
>If you have your function return a boost::python::ref the memory leak will most
>likely go away. I think you can do it like this (untested):
>
>   boost::python::ref get_as_pyobject(const std::string& country)
>   {
>     std::auto_ptr<hello> p(new hello(country));
>     return boost::python::make_ref(p);
>   }
>
>
>
>Ralf
>
>P.S.: This is an old known problem that never got resolved. It is not an issue
>anymore in the upcoming V2 relelease.
>
Yes, this worked, although I had to use
        boost::python::ref(boost::python::to_python(p)),
as make_ref() expects a const object.

Thank you so much - I was losing my last hair over this...

I will try the V2 release in due course.

Oliver

--------------------------
F. Oliver Gathmann, Ph.D.
Cenix BioScience GmbH
Pfotenhauer Strasse 108
D-01307 Dresden, Germany
phone: +49 (351) 210-2735
fax:   +49 (351) 210-1309
--------------------------







More information about the Cplusplus-sig mailing list