summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2007-01-25 19:19:35 (GMT)
committerThomas Heller <theller@ctypes.org>2007-01-25 19:19:35 (GMT)
commit817b489422f15b2aeee132c24bdc94d8de2b6ed9 (patch)
tree9ed7e72ea6e0175826205e860b198ad58531ac4c
parent564c9f5fb0616c60bd6111f8d4323ab4d661a528 (diff)
downloadcpython-817b489422f15b2aeee132c24bdc94d8de2b6ed9.zip
cpython-817b489422f15b2aeee132c24bdc94d8de2b6ed9.tar.gz
cpython-817b489422f15b2aeee132c24bdc94d8de2b6ed9.tar.bz2
Merged revisions 53556 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk/Modules/_ctypes ........ r53556 | thomas.heller | 2007-01-25 19:34:14 +0100 (Do, 25 Jan 2007) | 3 lines 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 8cff951..21742f3 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -154,6 +154,8 @@ Extension Modules
Library
-------
+- Patch #1643874: memory leak in ctypes fixed.
+
- Bug #1598181: Avoid O(N**2) bottleneck in subprocess communicate().
- Patch #1627441: close sockets properly in urllib2.
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index c16a387..31c3b99 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1424,10 +1424,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) {
@@ -1455,15 +1464,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;
}