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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue Aug 6 17:43:12 CEST 2002


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


__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com




More information about the Cplusplus-sig mailing list