Skip to content

advice as to why the same cppyy code can not be run multiple times in succession in the same run #269

@SimonDev666

Description

@SimonDev666

Hello,

i have embedded the python interpreter within my c++ program / app (QT) using pybind11.
i setup a simple python script file which internally sets up and then calls a simple c++ function using cppyy.
Runs without a problem.
now copy the same code, keeping to scope rules, so it does the same thing in an excution twice, then it fails on the second call that worked on the first call.
here is the error:

"Test001
Hello, World!
['/home/simon/Desktop/Code/VirtEnv/lib/python3.12/site-packages/', '/home/simon/Code/QT/Test_pybind11/Test_pybind11_cppyy_001/', '', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/usr/local/lib/python3.12/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.12/dist-packages']
here
done
free(): invalid size
Test002
*** Break *** abort
#0 0x00007f08d6d10893 in __GI___wait4 (pid=10578, stat_loc=stat_loc
entry=0x7ffc9b684228, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1 0x00007f08d6d109e7 in _GI___waitpid (pid=, stat_loc=stat_loc.........
#51 0x00007f08d737f94f in PyObject_CallFunction () from /lib/x86_64-linux-gnu/libpython3.12.so.1.0
#52 0x00007f08d74cf10a in PyImport_Import () from /lib/x86_64-linux-gnu/libpython3.12.so.1.0
#53 0x00007f08d74cf31f in PyImport_ImportModule () from /lib/x86_64-linux-gnu/libpython3.12.so.1.0
#54 0x000055dee4fe9934 in pybind11::module
::import (name=0x55dee501113e "Python_001") at /usr/include/pybind11/pybind11.h:1279
#55 0x000055dee4fe85f6 in main (argc=1, argv=0x7ffc9b688888) at /home/simon/Code/QT/Test_pybind11/Test_pybind11_cppyy_001/main.cpp:65"

here is the main c++ program / app (this is in my code and is compiled within QT creator):
" std::cout << "Test001\n";
{
//
py::scoped_interpreter guard{};
//
QString qPythonDir("/home/simon/Code/QT/Test_pybind11/Test_pybind11_cppyy_001/");
//
QString qPythonCommand("");
auto locals = py::dict();
//
qPythonCommand += "import sys\n";
qPythonCommand += "sys.path.insert(0, '";
qPythonCommand += qPythonDir.toStdString().c_str();
qPythonCommand += "')\n";
qPythonCommand += "sys.path.insert(0, '/home/simon/Desktop/Code/VirtEnv/lib/python3.12/site-packages/')\n";
qPythonCommand += "print(sys.path)\n";
py::exec(qPythonCommand.toStdString().c_str(), py::globals(), locals);
//
py::module module = py::module_::import("Python_001");
//
module.attr("runCPP")();
}
std::cout << "Test002\n";
{
//
py::scoped_interpreter guard{};
//
QString qPythonDir("/home/simon/Code/QT/Test_pybind11/Test_pybind11_cppyy_001/");
//
QString qPythonCommand("");
auto locals = py::dict();
//
qPythonCommand += "import sys\n";
qPythonCommand += "sys.path.insert(0, '";
qPythonCommand += qPythonDir.toStdString().c_str();
qPythonCommand += "')\n";
qPythonCommand += "sys.path.insert(0, '/home/simon/Desktop/Code/VirtEnv/lib/python3.12/site-packages/')\n";
qPythonCommand += "print(sys.path)\n";
py::exec(qPythonCommand.toStdString().c_str(), py::globals(), locals);
//
py::module module = py::module_::import("Python_001");
//
module.attr("runCPP")();
}
"

here is the python script (Python_001.py) called by pybind11:
"import cppyy

cppyy.cppdef(r"""
#include
void hello() {
std::cout << "Hello, World!" << std::endl;
}
""")

def runCPP():
print("here")
cppyy.gbl.hello()
print("done")
"

my cmake file looks like:
"cmake_minimum_required(VERSION 3.16)
project(Test_pybind11_cppyy_001 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
find_package(pybind11 REQUIRED)
add_executable(Test_pybind11_cppyy_001
main.cpp
../../EMB.h
)
#target_link_libraries(Test_pybind11_cppyy_001 Qt${QT_VERSION_MAJOR}::Core)
target_link_libraries(Test_pybind11_cppyy_001 Qt${QT_VERSION_MAJOR}::Core pybind11::embed)
include(GNUInstallDirs)
install(TARGETS Test_pybind11_cppyy_001
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}"

can anyone tell me what i am doing incorrect please.
this is all running on the latest Kali Linux, using the latest QT, with all python libaries uptodate as possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions