Skip to content

Commit

Permalink
initialize_finalize_loop experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Nov 17, 2022
1 parent 3cbbeeb commit 0e7ebbd
Show file tree
Hide file tree
Showing 4 changed files with 1,162 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pybind11/initialize_finalize_loop/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /usr/bin/bash
set -e
set -x
MAIN=initialize_finalize_loop
clang++ -o $MAIN.o -c -std=c++17 -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wnon-virtual-dtor -Wunused-result -Werror -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -I$HOME/forked/pybind11/include -I/usr/include/python3.10 $MAIN.cpp
clang++ -o $MAIN -rdynamic -O0 -g $MAIN.o -lpython3.10 -lpthread -ldl -lutil
34 changes: 34 additions & 0 deletions pybind11/initialize_finalize_loop/initialize_finalize_loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ./build.sh
./initialize_finalize_loop
top -p `pgrep initialize_fina` -b # NO leaking with Python 3.10.7
# Output in top_no_wrap_class.txt
./initialize_finalize_loop wrap_class
top -p `pgrep initialize_fina` -b # Leaking even with Pytyhon 3.10.7
# Output in top_wrap_class.txt
*/

#include <pybind11/embed.h>
#include <pybind11/pybind11.h>

namespace py = pybind11;

int main(int argc, char *[]) {
bool wrap_class = (argc > 1);
while (true) {
py::initialize_interpreter();
{
auto imom = py::reinterpret_steal<py::object>(PyModule_New("in_memory_only_module"));
if (!imom) {
throw py::error_already_set();
}
if (wrap_class) {
struct empty {};
py::class_<empty>(imom, "empty");
}
}
py::finalize_interpreter();
}
return 0;
}
Loading

0 comments on commit 0e7ebbd

Please sign in to comment.