Commit b41b1448 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

python: add proper reference counting for logged Components/Nodes

Former-commit-id: 94d532fd
parent 9c7076af
......@@ -41,6 +41,8 @@ namespace Python {
struct Logger {
PyObject_HEAD
std::vector<PyObject *> refs;
DPsim::DataLogger::Ptr logger;
const char *filename;
......
......@@ -36,8 +36,10 @@ PyObject* Python::Logger::newfunc(PyTypeObject *type, PyObject *args, PyObject *
self = (Python::Logger*) type->tp_alloc(type, 0);
if (self) {
using PyObjectVector = std::vector<PyObject *>;
using SharedLoggerPtr = std::shared_ptr<DPsim::DataLogger>;
new (&self->refs) PyObjectVector();
new (&self->logger) SharedLoggerPtr();
}
......@@ -46,9 +48,14 @@ PyObject* Python::Logger::newfunc(PyTypeObject *type, PyObject *args, PyObject *
void Python::Logger::dealloc(Python::Logger* self)
{
using PyObjectVector = std::vector<PyObject *>;
using SharedLoggerPtr = std::shared_ptr<DPsim::DataLogger>;
for (PyObject *pyRef : self->refs)
Py_DECREF(pyRef);
self->logger.~SharedLoggerPtr();
self->refs.~PyObjectVector();
Py_TYPE(self)->tp_free((PyObject*) self);
}
......@@ -103,6 +110,9 @@ PyObject* Python::Logger::logAttribute(Logger* self, PyObject* args, PyObject *k
return nullptr;
}
self->refs.push_back(pyObj);
Py_INCREF(pyObj);
Py_RETURN_NONE;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment