Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug a problem in tk85.dll in an application that embeds the Python interpreter

My C++ application embeds the Python interpreter, but seems to be having some trouble when it shuts down. Right after the main window closes, I get a segmentation fault (this is Windows, but we'll call it a segmentation fault anyway). The stack trace is below:

#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 ??() (??:??)

Where would I even begin to debug this problem? It seems to be reproducible.

like image 363
Nathan Osman Avatar asked Mar 25 '10 05:03

Nathan Osman


1 Answers

First, I let you know that I identified race conditions in Python's Tkinter when used with nonthreaded Tcl/Tk (Py2 is shipped with that) and proposed a fix. I'm not sure I fixed all the possible race conditions but I did fix all that I have run into.

Now, to be able to debug Tcl/Tk issues, you need to build Python with a debug version of Tcl/Tk and embed that. This should give you the ability to look into tk*.dll in debugger and see what's wrong.

  • Get the source code for your Python version and make the following changes:

    --- 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
    
  • run PCBuild\prepare_tcltk.bat from VS command prompt to download and build Tcl/Tk from source

    • Note that Tk (and Tix) can't be built with recent versions of WinSDK and will need to be patched. So run the .bat (it will fail), patch the downloaded sources and run it again.
  • Now build a debug Python as usual (PCBuild\readme.txt has the instructions).

like image 172
ivan_pozdeev Avatar answered Nov 08 '22 01:11

ivan_pozdeev