Skip to content

gh-148484: Fix memory leak of iterator in array.array constructor#148523

Open
gleb-pp wants to merge 4 commits intopython:mainfrom
gleb-pp:main
Open

gh-148484: Fix memory leak of iterator in array.array constructor#148523
gleb-pp wants to merge 4 commits intopython:mainfrom
gleb-pp:main

Conversation

@gleb-pp
Copy link
Copy Markdown
Contributor

@gleb-pp gleb-pp commented Apr 13, 2026

Fix memory leak of iterator in array.array constructor

When array.array is initialized with an arbitrary iterable, a Python iterator object is created via PyObject_GetIter().

If an error occurs later in array initialization (e.g. during element insertion or sequence conversion), the iterator was not properly decref'd, leading to a memory leak.

This patch ensures that the iterator is correctly DECREF'ed on all error paths after it is created.

The issue affects the fallback iterable initialization path only. Fast-path initializers (list, tuple, bytes, bytearray, array, str) are not affected.

Copy link
Copy Markdown
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch ensures that the iterator is correctly DECREF'ed on all error paths after it is created.

That's not true, you ignored some case. For example, shown in the issue thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants