Python 3.6 Embedded

matheusdalmago10 at gmail.com matheusdalmago10 at gmail.com
Wed May 13 15:51:51 EDT 2020


On Monday, December 26, 2016 at 5:27:33 AM UTC-2, jumppan... at gmail.com wrote:
> 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?

Three and a half year later and I have the exact same problem. Curios world.


More information about the Python-list mailing list