Python 3.6 Embedded

jumppanen.jussi at gmail.com jumppanen.jussi at gmail.com
Mon Dec 26 02:27:08 EST 2016


I'm using the python3.6 DLL to embed Python 3 inside a Windows application.

Here is some background into how Python is being used.

1. Firstly the same basic code worked fine with the earlier Python 2.7 version.

2. The code is structured as follows:

    Windows Executable
        +
        |
        + Scripting DLL
              +
              |
              + Pthyhon 3.6 DLL

In other words the executable talks to the Scripting DLL and the scripting DLL then talks to the Python DLL.

3. To run a script the following flow control is used:

    a. Application loads the Scripting DLL
        i.  The Scripting DLL calls Py_Initialize found in the Python DLL

    b. Application run the script file using the Scripting DLL
        i.  The Scripting DLL calls PyRun_SimpleFileEx in the Python DLL to run the script

    c. Unload the scripting DLL
        i.  The Scripting DLL calls Py_FinalizeEx in the Python DLL clean up

Symptoms of the Problem
-----------------------
The problem can be replicated by a script that contains nothing but a single import statement.

Using this test script the script can be run and re-run any number of times:

    import sys

Using this test script the script will run the first time but fail every subsequent time:

    import socket

The difference in the two import statements is the former is a built-in module while the later is using a dynamically loaded module.

The Cause of the Problem
------------------------
I tracked down the cause of the problem to the fact the python36.dll unloads just fine for the first case but fails to unloaded for the second case.

So when the application loads and unloads the Scripting DLL the python36.dll remains loaded.

Then when the python36.dll is used the second time it now contains pointers to stale data and crashes on the second run.

The Py_FinalizeEx document found here suggests it should stop all interpreters: https://docs.python.org/dev/c-api/init.html

Anyone have an idea know what might be going wrong?

What code is holding on to the Python DLL?


More information about the Python-list mailing list