Skip to content

Commit 18da8ad

Browse files
committed
Solve issue with memory references in python.
1 parent 390282f commit 18da8ad

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

source/loaders/py_loader/source/py_loader_dict.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ void py_loader_impl_dict_debug(PyObject *py_dict)
168168
return;
169169
}
170170

171+
printf("Dictionary %p:\n", (void *)py_dict);
172+
171173
while (PyDict_Next(py_dict, &pos, &key, &value))
172174
{
173175
printf("Key: ");
174176
PyObject_Print(key, stdout, 0);
175177
printf("#%ld, Value: ", Py_REFCNT(key));
176178
PyObject_Print(value, stdout, 0);
177179
printf(" #%ld\n", Py_REFCNT(value));
178-
printf("\n");
179180
fflush(stdout);
180181
}
181182
}
@@ -202,6 +203,8 @@ PyObject *py_loader_impl_finalizer_wrap_dict(PyObject *obj, void *v)
202203
union py_loader_impl_dict_cast dict_cast = { &py_loader_impl_dict_type };
203204
PyObject *args, *wrapper;
204205
struct py_loader_impl_dict_obj *wrapper_obj;
206+
PyObject *key, *value;
207+
Py_ssize_t pos = 0;
205208

206209
py_loader_thread_acquire();
207210

@@ -225,5 +228,12 @@ PyObject *py_loader_impl_finalizer_wrap_dict(PyObject *obj, void *v)
225228
wrapper_obj->v = v;
226229
wrapper_obj->parent = obj;
227230

231+
/* At this point the references are incremented due to the copy, so we need to decrement them */
232+
while (PyDict_Next(obj, &pos, &key, &value))
233+
{
234+
Py_DecRef(key);
235+
Py_DecRef(value);
236+
}
237+
228238
return wrapper;
229239
}

0 commit comments

Comments
 (0)