125
|
1
|
|
2 Note about the "memory leaks" in the Python allocators
|
|
3 ======================================================
|
|
4
|
|
5 This concerns functions "PyTuple_SetItem()" and
|
|
6 "PyDict_SetItemString()", whose documentation is the following:
|
|
7
|
|
8 >>>>>
|
|
9 * int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
|
|
10
|
|
11 Insert a reference to object o at position pos of the tuple pointed
|
|
12 to by p. Return 0 on success. If pos is out of bounds, return -1 and
|
|
13 set an IndexError exception.
|
|
14
|
|
15 Note: This function "steals" a reference to o and discards a
|
|
16 reference to an item already in the tuple at the affected position.
|
|
17
|
|
18 * int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
|
|
19
|
|
20 Insert val into the dictionary p using key as a key. key should be a
|
|
21 const char* UTF-8 encoded bytes string. The key object is created
|
|
22 using PyUnicode_FromString(key). Return 0 on success or -1 on
|
|
23 failure. This function does not steal a reference to val.
|
|
24 <<<<<
|
|
25
|
|
26
|
|
27 As can be seen:
|
|
28
|
|
29 * PyTuple_SetItem() steals the reference.
|
|
30
|
|
31 * PyDict_SetItemString() does *not* steal the reference.
|
|
32
|
|
33
|
|
34 Consequently, when passing an object created by the Orthanc Python
|
|
35 plugin:
|
|
36
|
|
37 * PyTuple_SetItem() must use "PythonObject::Release()".
|
|
38
|
|
39 * PyDict_SetItemString() must use "PythonObject::GetPyObject()".
|
|
40
|
|
41
|
|
42 In releases <= 4.0 of the Python plugin, PyDict_SetItemString() was
|
|
43 using "PythonObject::Release()" instead of
|
|
44 "PythonObject::GetPyObject()", which resulted in memory leaks.
|