调试嵌入Python解释器的应用中的tk85.dll问题
我的C++应用程序嵌入了Python解释器,但在关闭时似乎遇到了一些问题。在主窗口关闭后,我遇到了一个段错误(虽然这是在Windows上,但我们还是叫它段错误)。下面是错误的堆栈跟踪信息:
#0 102AD580 tk85!Tk_MainWindow() (C:\Users\... 1.3\bin\Debug\lib\tk85.dll:??)
#1 103082DD tk85!XSetStipple() (C:\Users\... 1.3\bin\Debug\lib\tk85.dll:??)
#2 102214A3 ??() (C:\Users\...1.3\bin\Debug\lib\tk85.dll:??)
#3 10220000 ??() (??:??)
#4 00000000 ??() (??:??)
我该从哪里开始调试这个问题呢?看起来这个问题是可以重复出现的。
1 个回答
1
首先,我想告诉你,我发现了在使用非线程版的Tcl/Tk时,Python的Tkinter存在竞争条件的问题(Python 2自带这个)。我提出了一个修复方案。虽然我不确定是否修复了所有可能的竞争条件,但我解决了我遇到的所有问题。
现在,要调试Tcl/Tk的问题,你需要用调试版本的Tcl/Tk来构建Python,并把它嵌入进去。这样你就可以在调试器中查看tk*.dll
,看看哪里出错了。
获取你所用Python版本的源代码,并进行以下更改:
--- a/PCbuild/prepare_tcltk.bat +++ b/PCbuild/prepare_tcltk.bat @@ -46,10 +46,10 @@ rem if ERRORLEVEL 1 (echo Cannot locate python.exe on PATH or as PYTHON variable call "%PCBUILD%\get_externals.bat" --tkinter-src %ORG_SETTING% -%MSBUILD% "%PCBUILD%\tcl.vcxproj" /p:Configuration=Release /p:Platform=Win32 -%MSBUILD% "%PCBUILD%\tk.vcxproj" /p:Configuration=Release /p:Platform=Win32 -%MSBUILD% "%PCBUILD%\tix.vcxproj" /p:Configuration=Release /p:Platform=Win32 +%MSBUILD% "%PCBUILD%\tcl.vcxproj" /p:Configuration=Debug /p:Platform=Win32 +%MSBUILD% "%PCBUILD%\tk.vcxproj" /p:Configuration=Debug /p:Platform=Win32 +%MSBUILD% "%PCBUILD%\tix.vcxproj" /p:Configuration=Debug /p:Platform=Win32 -%MSBUILD% "%PCBUILD%\tcl.vcxproj" /p:Configuration=Release /p:Platform=x64 -%MSBUILD% "%PCBUILD%\tk.vcxproj" /p:Configuration=Release /p:Platform=x64 -%MSBUILD% "%PCBUILD%\tix.vcxproj" /p:Configuration=Release /p:Platform=x64 +%MSBUILD% "%PCBUILD%\tcl.vcxproj" /p:Configuration=Debug /p:Platform=x64 +%MSBUILD% "%PCBUILD%\tk.vcxproj" /p:Configuration=Debug /p:Platform=x64 +%MSBUILD% "%PCBUILD%\tix.vcxproj" /p:Configuration=Debug /p:Platform=x64
在VS命令提示符下运行
PCBuild\prepare_tcltk.bat
,从源代码下载并构建Tcl/Tk。- 注意,Tk(和Tix)无法用最近版本的WinSDK构建,需要进行补丁处理。所以先运行这个
.bat
文件(它会失败),然后对下载的源代码进行补丁处理,再运行一次。
- 注意,Tk(和Tix)无法用最近版本的WinSDK构建,需要进行补丁处理。所以先运行这个
现在像往常一样构建一个调试版的Python(
PCBuild\readme.txt
里有说明)。