[C++-sig] Memory Leaks In VS.NET 2003 With BOOST_PYTHON_MODULE

David Abrahams dave at boost-consulting.com
Thu Oct 13 16:37:10 CEST 2005

<mjkeyes at sbcglobal.net> writes:

> Hey again, I'm back.
> I've moved on to building an extension dll file in order to supplement
> my embedded application.  Once again, I've got memory leaks.
> However, this is an easy one to test using your method of calling a
> function over and over.  I did this and it chewed up the memory in my
> system like a hog (at least in task manager I see Python.exe growing
> steadily in memory consumption).
> Here is some pseudo-code of what I'm doing:

Not the actual code?  Please post actual, minimal code that fails.

> class A
> {
> public:
>   void SetString(const std::string &sVal)
>   {
>     m_sString = sVal; <--- Memory leak

I very much doubt that this line actually leaks memory unless there's
a bug in your compiler's standard library implementation.

>   }
>   void DoStuff()
>   {
>     SetString(msc_sString);
>   }
> protected:
>   static const std::string msc_sString;
> }
> class B
> {
> public:
>   object CreateClassA()
>   {
>     return object(new CreateClassA()); <---- memory leak

AFAICT this one isn't even legal C++, so I doubt it leaks memory also ;-)


         object(new X)

definitely leaks memory, for any X.  When converting a pointer to
Python, by default the pointee is copied into a new Python object, so
the original pointer you dynamically allocated will always be leaked.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list