summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2007-01-25 18:34:14 (GMT)
committerThomas Heller <theller@ctypes.org>2007-01-25 18:34:14 (GMT)
commit4378215474361ea0f94341275285a856970aa2b8 (patch)
tree8d4c29e3067dee8e029d294e7a892aa572ec3d97
parent391e917b1a0670349282e96ac9d5aa3014df4197 (diff)
downloadcpython-4378215474361ea0f94341275285a856970aa2b8.zip
cpython-4378215474361ea0f94341275285a856970aa2b8.tar.gz
cpython-4378215474361ea0f94341275285a856970aa2b8.tar.bz2
Fix for #1643874: When calling SysAllocString, create a PyCObject
which will eventually call SysFreeString to free the BSTR resource.
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/_ctypes/cfield.c29
2 files changed, 23 insertions, 8 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index ad72b08..3fad862 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,8 @@ Core and builtins
Library
-------
+- Patch #1643874: memory leak in ctypes fixed.
+
- Patch #1627441: close sockets properly in urllib2.
- Bug #494589: make ntpath.expandvars behave according to its docstring.
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index ad83195..799b457 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1432,10 +1432,19 @@ Z_get(void *ptr, unsigned size)
#endif
#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 *
BSTR_set(void *ptr, PyObject *value, unsigned size)
{
BSTR bstr;
+ PyObject *result;
/* convert value into a PyUnicodeObject or NULL */
if (Py_None == value) {
@@ -1463,15 +1472,19 @@ BSTR_set(void *ptr, PyObject *value, unsigned size)
} else
bstr = NULL;
- /* free the previous contents, if any */
- if (*(BSTR *)ptr)
- SysFreeString(*(BSTR *)ptr);
-
- /* and store it */
- *(BSTR *)ptr = bstr;
+ if (bstr) {
+ result = PyCObject_FromVoidPtr((void *)bstr, _my_SysFreeString);
+ if (result == NULL) {
+ SysFreeString(bstr);
+ return NULL;
+ }
+ } else {
+ result = Py_None;
+ Py_INCREF(result);
+ }
- /* We don't need to keep any other object */
- _RET(value);
+ *(BSTR *)ptr = bstr;
+ return result;
}