[Python-checkins] r53558 - in python/branches/release25-maint: Misc/NEWS Modules/_ctypes Modules/_ctypes/cfield.c

thomas.heller python-checkins at python.org
Thu Jan 25 20:19:35 CET 2007


Author: thomas.heller
Date: Thu Jan 25 20:19:35 2007
New Revision: 53558

Modified:
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Modules/_ctypes/   (props changed)
   python/branches/release25-maint/Modules/_ctypes/cfield.c
Log:
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.
........



Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Thu Jan 25 20:19:35 2007
@@ -154,6 +154,8 @@
 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.

Modified: python/branches/release25-maint/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/release25-maint/Modules/_ctypes/cfield.c	(original)
+++ python/branches/release25-maint/Modules/_ctypes/cfield.c	Thu Jan 25 20:19:35 2007
@@ -1424,10 +1424,19 @@
 #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 @@
 	} 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;
 }
 
 


More information about the Python-checkins mailing list