Commit 43782154 authored by Thomas Heller's avatar Thomas Heller

Fix for #1643874: When calling SysAllocString, create a PyCObject

which will eventually call SysFreeString to free the BSTR resource.
parent 391e917b
...@@ -123,6 +123,8 @@ Core and builtins ...@@ -123,6 +123,8 @@ Core and builtins
Library Library
------- -------
- Patch #1643874: memory leak in ctypes fixed.
- Patch #1627441: close sockets properly in urllib2. - Patch #1627441: close sockets properly in urllib2.
- Bug #494589: make ntpath.expandvars behave according to its docstring. - Bug #494589: make ntpath.expandvars behave according to its docstring.
......
...@@ -1432,10 +1432,19 @@ Z_get(void *ptr, unsigned size) ...@@ -1432,10 +1432,19 @@ Z_get(void *ptr, unsigned size)
#endif #endif
#ifdef MS_WIN32 #ifdef MS_WIN32
/* We cannot use SysFreeString as the PyCObject_FromVoidPtr
because of different calling convention
*/
static void _my_SysFreeString(void *p)
{
SysFreeString((BSTR)p);
}
static PyObject * static PyObject *
BSTR_set(void *ptr, PyObject *value, unsigned size) BSTR_set(void *ptr, PyObject *value, unsigned size)
{ {
BSTR bstr; BSTR bstr;
PyObject *result;
/* convert value into a PyUnicodeObject or NULL */ /* convert value into a PyUnicodeObject or NULL */
if (Py_None == value) { if (Py_None == value) {
...@@ -1463,15 +1472,19 @@ BSTR_set(void *ptr, PyObject *value, unsigned size) ...@@ -1463,15 +1472,19 @@ BSTR_set(void *ptr, PyObject *value, unsigned size)
} else } else
bstr = NULL; bstr = NULL;
/* free the previous contents, if any */ if (bstr) {
if (*(BSTR *)ptr) result = PyCObject_FromVoidPtr((void *)bstr, _my_SysFreeString);
SysFreeString(*(BSTR *)ptr); if (result == NULL) {
SysFreeString(bstr);
/* and store it */ return NULL;
*(BSTR *)ptr = bstr; }
} else {
result = Py_None;
Py_INCREF(result);
}
/* We don't need to keep any other object */ *(BSTR *)ptr = bstr;
_RET(value); return result;
} }
......
Markdown is supported
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