Skip to content

Commit d5c8aab

Browse files
committed
Fixed ref counting in CPointer::__del__
- Destructors of custom types are now called
1 parent e0ee2d0 commit d5c8aab

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

addons/source-python/packages/source-python/memory/manager.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ def __init__(self, *args, wrap=True, auto_dealloc=False):
100100
raise ValueError(
101101
'No constructor was specified, but arguments were passed.')
102102

103+
def on_dealloc(self):
104+
'''
105+
This method is automatically called, when the pointer gets
106+
deallocated. It then calls the destructor if it was specified.
107+
'''
108+
109+
# Call the destructor if it was specified
110+
if self._destructor is not None:
111+
self._destructor()
112+
103113

104114
# =============================================================================
105115
# >> TypeManager
@@ -264,7 +274,7 @@ def create_type_from_file(self, type_name, f, bases=(CustomType,)):
264274
(Key.SIZE, int, CustomType._size)
265275
)
266276
))[0][1]
267-
277+
268278
cls_dict = dict(zip(('_binary', '_src_check', '_size'), data))
269279

270280
# Prepare pointer and instance attributes

src/core/modules/memory/memory_tools.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ void CPointer::PreRealloc(PyObject* self, int iSize)
226226

227227
void CPointer::__del__(PyObject* self)
228228
{
229-
if (PyObject_IsTrue(PyObject_GetAttrString(self, "auto_dealloc")))
229+
CPointer* ptr = extract<CPointer *>(self);
230+
if (ptr->m_bAutoDealloc)
230231
{
231-
unsigned long ulAddr = extract<unsigned long>(PyObject_GetAttrString(self, "address"));
232-
PythonLog(4, "[SP] Automatically deallocating pointer at %u.", ulAddr);
232+
PythonLog(4, "[SP] Automatically deallocating pointer at %u.", ptr->m_ulAddr);
233233
PreDealloc(self);
234234
}
235235
}

0 commit comments

Comments
 (0)