[python-win32] Memory Leak in MFC/C++ based Python Extended/Embedded code.

Mark Hammond mhammond at skippinet.com.au
Tue Jan 8 03:20:43 CET 2008


Python does allocate memory that is never freed - but in general, that is
not a "leak", just a 1-time allocation that lives for the life of the
program.  However, if you *repeatedly* call Py_Initialize/Finalize, I
believe some leaks will happen, and I believe there is an open bug report in
the Python tracker regarding this (but don't have the time to find it).

Assuming you just want to init and finalize once, then you can safely ignore
the MFC leak check output.  Something like 'valgrind' does differentiate
between leaks and 1-time allocations, so if you *really* need a leak
checker, you should be looking at something like that.

Cheers,

Mark

> -----Original Message-----
> From: python-win32-bounces at python.org [mailto:python-win32-
> bounces at python.org] On Behalf Of Dan Trowbridge
> Sent: Tuesday, 8 January 2008 1:07 PM
> To: python-win32 at python.org
> Subject: [python-win32] Memory Leak in MFC/C++ based Python
> Extended/Embedded code.
> 
> Hi all,
> 
> I have a MS Windows/MFC based C++ code that I have implemented Python
> embedding/extending in.  (I am using Visual Studio 2003.)  The code
> seems to
> do what I have intended except it always reports a memory leak when it
> exits.  I tried all kinds of things but there did not seem to be
> anything I
> could do to get rid of it.
> 
> So...
> 
> I built a new MFC project and the only things I added was...
> 
> 	#include <Python.h>
> 
> at the top of the application class .cpp file and the following two
> lines
> 
> 	Py_Initialize();
> 	Py_Finalize();
> 
> in the application class constructor.
> 
> The minimal code STILL reports a memory error.  It looks like the
> memory
> leak had nothing to do with my code.
> 
> Anybody have any ideas how to fix this?
> 
> Background: I am using Python 2.5.1.  I am not using BOOST::PYTHON (I
> am
> using BOOST::NUMERIC and BOOST::SHARED_PTR for other things in the code
> -
> maybe I should use BOOST::PYTHON - maybe their shared_ptr's would take
> care
> of this for me? ).  I also am not using SWIG or any other "helpers".
> 
> Any help would be greatly appreciated.  If this is not the right forum
> please advise where to post this.
> 
> It got to figure that someone has seen this before - considering as
> long as
> Python has been around and as big as the developer community is.
> 
> Thanks in advance for your collective help.
> 
> Dannyt
> 
> 
> 
> 
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32



More information about the python-win32 mailing list