Explorar el Código

added hook into garbage collector

Kolten Pearson hace 2 años
padre
commit
2c96dbe7ea
Se han modificado 2 ficheros con 8 adiciones y 1 borrados
  1. 6 0
      c_bindings/pocketpy_c.cpp
  2. 2 1
      run_c_binding_test.sh

+ 6 - 0
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<PKPY_STACK_SIZE>();
+    vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex;
     return (pkpy_vm*) vm;
 }
 

+ 2 - 1
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