From 2c96dbe7ea0fe3e712cf6f3997e595cafc5d8d74 Mon Sep 17 00:00:00 2001 From: Kolten Pearson Date: Tue, 2 May 2023 22:18:03 -0600 Subject: [PATCH] added hook into garbage collector --- c_bindings/pocketpy_c.cpp | 6 ++++++ run_c_binding_test.sh | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/c_bindings/pocketpy_c.cpp b/c_bindings/pocketpy_c.cpp index fdbace28..526910dc 100644 --- a/c_bindings/pocketpy_c.cpp +++ b/c_bindings/pocketpy_c.cpp @@ -44,6 +44,7 @@ class CVM : public VM { } ~CVM() { + c_data->clear(); delete c_data; } }; @@ -95,9 +96,14 @@ bool pkpy_clear_error(pkpy_vm* vm_handle, char** message) { SAFEGUARD_CLOSE } +void gc_marker_ex(CVM* vm) { + for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj); +} + pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) { CVM* vm = new CVM(use_stdio, enable_os); vm->c_data = new ValueStackImpl(); + vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex; return (pkpy_vm*) vm; } diff --git a/run_c_binding_test.sh b/run_c_binding_test.sh index 33018288..4afb34cd 100644 --- a/run_c_binding_test.sh +++ b/run_c_binding_test.sh @@ -12,7 +12,8 @@ echo "compiling c executable" clang -c -o test.o c_bindings/test.c -Wfatal-errors -O2 -Wall -Wno-sign-compare -Wno-unused-variable -I src/ -fsanitize=address -g echo "linking" clang++ -o c_binding_test test.o pocketpy_c.o -stdlib=libc++ -fsanitize=address -g -echo "running, no weird output should show up" +echo "running, leaksanitizer is finding a false postive leak in the CVM constructor" +echo "ignore that but pay attention to anything else" ./c_binding_test > binding_test_scratch echo "checking results (they should be identical)" diff -q -s binding_test_scratch c_bindings/test_answers.txt