boostorg / python

Boost.org python module
http://boostorg.github.io/python
Boost Software License 1.0
465 stars 201 forks source link

`import("__main__")` crashes on Windows in Debug mode #405

Closed stephengtuggy closed 1 year ago

stephengtuggy commented 1 year ago

As reported here:

Running boost::python::import("__main__"); crashes on Windows, specifically when compiled in Debug configuration (not Release). This was on Windows 11 x64, with Python 3.10, I believe. Others have reported the same issue with other versions of Windows and of Python. This issue does not exist on Linux, as far as I can tell.

A small program illustrating this bug can be found here.

Thanks.

Osyotr commented 1 year ago

@stephengtuggy you might need to add

target_compile_definitions(${target_name} PUBLIC "$<$<CONFIG:Debug>:BOOST_DEBUG_PYTHON>")
target_compile_definitions(${target_name} PUBLIC "$<$<CONFIG:Debug>:Py_DEBUG>")

to your CMakeLists.txt.

stephengtuggy commented 1 year ago

@stephengtuggy you might need to add

target_compile_definitions(${target_name} PUBLIC "$<$<CONFIG:Debug>:BOOST_DEBUG_PYTHON>")
target_compile_definitions(${target_name} PUBLIC "$<$<CONFIG:Debug>:Py_DEBUG>")

to your CMakeLists.txt.

@Osyotr Hmm. Interesting. When I make those changes, I am no longer able to reproduce the crash. At least not as reliably.

Any idea what makes the difference?

Osyotr commented 1 year ago

ABI incompatibility. https://www.boost.org/doc/libs/1_81_0/libs/python/doc/html/building/python_debugging_builds.html

stephengtuggy commented 1 year ago

I see. I had BOOST_DEBUG_PYTHON in place, but not Py_DEBUG. Also, I was setting the compile definitions on the directory level, not per-target. Not sure if the latter makes a significant difference or not.

stephengtuggy commented 1 year ago

Closing, since @Osyotr 's solution worked. Thanks!