Pārlūkot izejas kodu

py_panda: fix leak of reference counted class with inaccessible dtor

If a class inherits from ReferenceCount, it can be destructed by downcasting to ReferenceCount, so it should not be an obstacle to properly clean it up when such a class is returned from C++.

This issue comes up when a CycleData is returned via MemoryUsagePointers, which is not exposed so is wrapped as NodeReferenceCount instead, which has a protected destructor.
rdb 7 gadi atpakaļ
vecāks
revīzija
0367c73026
1 mainītis faili ar 11 papildinājumiem un 1 dzēšanām
  1. 11 1
      dtool/src/interrogatedb/py_panda.h

+ 11 - 1
dtool/src/interrogatedb/py_panda.h

@@ -158,6 +158,16 @@ static void Dtool_FreeInstance_##CLASS_NAME(PyObject *self) {\
   Py_TYPE(self)->tp_free(self);\
 }
 
+#define Define_Dtool_FreeInstanceRef_Private(CLASS_NAME,CNAME)\
+static void Dtool_FreeInstance_##CLASS_NAME(PyObject *self) {\
+  if (DtoolInstance_VOID_PTR(self) != nullptr) {\
+    if (((Dtool_PyInstDef *)self)->_memory_rules) {\
+      unref_delete((ReferenceCount *)(CNAME *)DtoolInstance_VOID_PTR(self));\
+    }\
+  }\
+  Py_TYPE(self)->tp_free(self);\
+}
+
 #define Define_Dtool_Simple_FreeInstance(CLASS_NAME, CNAME)\
 static void Dtool_FreeInstance_##CLASS_NAME(PyObject *self) {\
   ((Dtool_InstDef_##CLASS_NAME *)self)->_value.~##CLASS_NAME();\
@@ -292,7 +302,7 @@ Define_Dtool_Class(MODULE_NAME,CLASS_NAME,PUBLIC_NAME)
 #define Define_Module_ClassRef_Private(MODULE_NAME,CLASS_NAME,CNAME,PUBLIC_NAME)\
 Define_Module_Class_Internal(MODULE_NAME,CLASS_NAME,CNAME)\
 Define_Dtool_new(CLASS_NAME,CNAME)\
-Define_Dtool_FreeInstance_Private(CLASS_NAME,CNAME)\
+Define_Dtool_FreeInstanceRef_Private(CLASS_NAME,CNAME)\
 Define_Dtool_Class(MODULE_NAME,CLASS_NAME,PUBLIC_NAME)
 
 #define Define_Module_ClassRef(MODULE_NAME,CLASS_NAME,CNAME,PUBLIC_NAME)\