Embedding Python in an MFC application

Mark Hammond mhammond at skippinet.com.au
Wed Apr 23 02:40:32 CEST 2003

Darin Willits wrote:
> Hi,
> I know that the first response to this will be "Why are you embedding
> when you could just extend?" but please believe that I have my reasons
> and for now this is what I have to do.  However I am something of a
> newbie with this stuff so bear with me.
> Now for the question:  I have created a simple MFC application (with
> AppWizzard) which mearly calls Py_Initialize() and Py_Finalize() in the
> OnInit function.  When I build and run this app it starts the python
> interpreter fine, and shuts it down without a noticable problem. 
> However when I close the application I get a huge memory leak.  Now for
> the wieird part.  If I switch the above application from linking to MFC
> as a shared dll, to statically linking to MFC the memory dump is gone. 
> Why is this?  This is driving me nuts as I really need to use the MFC
> as a shared dll.
> Please tell me I am missing something simple.  I am learning this stuff
> as I go and this one has me stumped.

You aren't seeing leaks, you are seeing memory that is allocated once by 
Python, and not freed.  MSVC doesn't distinguish between the two, 
whereas all commercial checkers do.  Just ignore them.  Note there is an 
issue that may cause a few problems when doing multiple Py_Init/Fini 
pairs - so just don't do that ;)


More information about the Python-list mailing list