[Python-checkins] GH-92804: Fix memory leak in memoryview iterator (gh-92805)

miss-islington webhook-mailer at python.org
Sat May 14 10:55:25 EDT 2022


https://github.com/python/cpython/commit/d9089c0e8b3728adba502389d193e80543741a2b
commit: d9089c0e8b3728adba502389d193e80543741a2b
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-05-14T07:55:03-07:00
summary:

GH-92804: Fix memory leak in memoryview iterator (gh-92805)

(cherry picked from commit d923fdf54bc97baece879179ba4971f632b9722b)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-05-14-13-22-11.gh-issue-92804.rAqpI2.rst
M Objects/memoryobject.c
M Objects/object.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-14-13-22-11.gh-issue-92804.rAqpI2.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-14-13-22-11.gh-issue-92804.rAqpI2.rst
new file mode 100644
index 0000000000000..7a5fd3f6568ea
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-14-13-22-11.gh-issue-92804.rAqpI2.rst	
@@ -0,0 +1 @@
+Fix memory leak in ``memoryview`` iterator as it was not finalized at exit. Patch by Kumar Aditya.
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 45fe8985c2adb..8c26916882447 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -3156,7 +3156,7 @@ static PyMethodDef memory_methods[] = {
 /*                          Memoryview Iterator                           */
 /**************************************************************************/
 
-static PyTypeObject PyMemoryIter_Type;
+PyTypeObject _PyMemoryIter_Type;
 
 typedef struct {
     PyObject_HEAD
@@ -3233,7 +3233,7 @@ memory_iter(PyObject *seq)
     }
 
     memoryiterobject *it;
-    it = PyObject_GC_New(memoryiterobject, &PyMemoryIter_Type);
+    it = PyObject_GC_New(memoryiterobject, &_PyMemoryIter_Type);
     if (it == NULL) {
         return NULL;
     }
@@ -3246,7 +3246,7 @@ memory_iter(PyObject *seq)
     return (PyObject *)it;
 }
 
-static PyTypeObject PyMemoryIter_Type = {
+PyTypeObject _PyMemoryIter_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     .tp_name = "memory_iterator",
     .tp_basicsize = sizeof(memoryiterobject),
diff --git a/Objects/object.c b/Objects/object.c
index d5f21b7c6aa19..303a22b6bfd01 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1845,6 +1845,7 @@ _PyTypes_InitState(PyInterpreterState *interp)
 extern PyTypeObject PyHKEY_Type;
 #endif
 extern PyTypeObject _Py_GenericAliasIterType;
+extern PyTypeObject _PyMemoryIter_Type;
 
 static PyTypeObject* static_types[] = {
     // The two most important base types: must be initialized first and
@@ -1944,6 +1945,7 @@ static PyTypeObject* static_types[] = {
     &_PyHamt_Type,
     &_PyInterpreterID_Type,
     &_PyManagedBuffer_Type,
+    &_PyMemoryIter_Type,
     &_PyMethodWrapper_Type,
     &_PyNamespace_Type,
     &_PyNone_Type,



More information about the Python-checkins mailing list