summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorLuke Garland <luke.garland01@gmail.com>2022-12-02 10:13:33 (GMT)
committerGitHub <noreply@github.com>2022-12-02 10:13:33 (GMT)
commitb027dd78bbdb66f2995bb898af304e66e5508bf6 (patch)
tree8186b09f41ea27821a8d8ff90152617c29af6736 /Modules
parent64dae2efd5a083d342d744d40ca8d6ebb28bc771 (diff)
downloadcpython-b027dd78bbdb66f2995bb898af304e66e5508bf6.zip
cpython-b027dd78bbdb66f2995bb898af304e66e5508bf6.tar.gz
cpython-b027dd78bbdb66f2995bb898af304e66e5508bf6.tar.bz2
bpo-40882: Fix a memory leak in SharedMemory on Windows (GH-20684)
In multiprocessing.shared_memory.SharedMemory(), the temporary view returned by MapViewOfFile() should be unmapped when it is no longer needed. (cherry picked from commit 85c128e34daec7625b74746e127afa25888ccde1) Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_winapi.c25
-rw-r--r--Modules/clinic/_winapi.c.h28
2 files changed, 52 insertions, 1 deletions
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index 9b30a90..f6bb07f 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -1403,6 +1403,30 @@ _winapi_MapViewOfFile_impl(PyObject *module, HANDLE file_map,
}
/*[clinic input]
+_winapi.UnmapViewOfFile
+
+ address: LPCVOID
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_winapi_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
+/*[clinic end generated code: output=4f7e18ac75d19744 input=8c4b6119ad9288a3]*/
+{
+ BOOL success;
+
+ Py_BEGIN_ALLOW_THREADS
+ success = UnmapViewOfFile(address);
+ Py_END_ALLOW_THREADS
+
+ if (!success) {
+ return PyErr_SetFromWindowsErr(0);
+ }
+
+ Py_RETURN_NONE;
+}
+
+/*[clinic input]
_winapi.OpenFileMapping -> HANDLE
desired_access: DWORD
@@ -2095,6 +2119,7 @@ static PyMethodDef winapi_functions[] = {
_WINAPI_READFILE_METHODDEF
_WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF
_WINAPI_TERMINATEPROCESS_METHODDEF
+ _WINAPI_UNMAPVIEWOFFILE_METHODDEF
_WINAPI_VIRTUALQUERYSIZE_METHODDEF
_WINAPI_WAITNAMEDPIPE_METHODDEF
_WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF
diff --git a/Modules/clinic/_winapi.c.h b/Modules/clinic/_winapi.c.h
index f568357..e3ed148 100644
--- a/Modules/clinic/_winapi.c.h
+++ b/Modules/clinic/_winapi.c.h
@@ -731,6 +731,32 @@ exit:
return return_value;
}
+PyDoc_STRVAR(_winapi_UnmapViewOfFile__doc__,
+"UnmapViewOfFile($module, address, /)\n"
+"--\n"
+"\n");
+
+#define _WINAPI_UNMAPVIEWOFFILE_METHODDEF \
+ {"UnmapViewOfFile", (PyCFunction)_winapi_UnmapViewOfFile, METH_O, _winapi_UnmapViewOfFile__doc__},
+
+static PyObject *
+_winapi_UnmapViewOfFile_impl(PyObject *module, LPCVOID address);
+
+static PyObject *
+_winapi_UnmapViewOfFile(PyObject *module, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ LPCVOID address;
+
+ if (!PyArg_Parse(arg, "" F_POINTER ":UnmapViewOfFile", &address)) {
+ goto exit;
+ }
+ return_value = _winapi_UnmapViewOfFile_impl(module, address);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(_winapi_OpenFileMapping__doc__,
"OpenFileMapping($module, desired_access, inherit_handle, name, /)\n"
"--\n"
@@ -1216,4 +1242,4 @@ _winapi__mimetypes_read_windows_registry(PyObject *module, PyObject *const *args
exit:
return return_value;
}
-/*[clinic end generated code: output=d76d0a5901db2e2a input=a9049054013a1b77]*/
+/*[clinic end generated code: output=acabf8f2b5cc44a1 input=a9049054013a1b77]*/