FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: macOS backend lacks several autorelease pools · Issue #31751 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: macOS backend lacks several autorelease pools #31751

Closed

Description

Bug summary

I'm investigating #31106.

At the time show(PyObject* self) is called, the window in question has a -retainCount of 600+. This is due to AppKit and other macOS frameworks calling [[theWindow retain] autorelease] across several internal methods.

The window is retained and added to the autorelease pool at the top of the stack, which is either the one set up in wait_for_stdin() or in [NSApp run]. In any case, the pool has never been drained by the time show() is called. Even after the user closes the window, the pool has still not been drained.

Generally, if you are calling from non-Objective-C code to Objective-C code, you need to set up an autorelease pool before crossing the boundary and release it after returning. Using an @autoreleasepool block makes this easy; however, it involves touching a lot of C functions inside of _macosx.m.

I'm not experienced with Python extension modules. Is there a way to have Python automatically call a function before and after calling the C function assigned to each PyMethodDef?

Code for reproduction

import matplotlib.pyplot as plt

for i in range(0, 4):
    plt.subplots()

plt.show()

Actual outcome

Each NSWindow still has a retain count of 600+ after closing.

Expected outcome

Each NSWindow hits a retain count of 0 on close and -dealloc is called.

Additional information

No response

Operating system

No response

Matplotlib Version

0.2.0.dev54528+unknown.gf4cf12592.d20260526

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. QuLogic commented on May 27, 2026

    Member

    I'm not experienced with Python extension modules. Is there a way to have Python automatically call a function before and after calling the C function assigned to each PyMethodDef?

    No, not unless there's some kind of decorator type thing that Objective C provides. You will need to place pools into each function that you think needs it, otherwise.

  2. iccir commented on May 27, 2026

    ContributorAuthor

    No, not unless there's some kind of decorator type thing that Objective C provides. You will need to place pools into each function that you think needs it, otherwise.

    Unfortunately, there isn't. I placed @autoreleasepool { } blocks at the start of each C function exposed to Python. With these blocks in place, the windows are now properly being dealloc'd upon close. However, this change will expose any place where an object is being over-released, so more testing is required.

  3. iccir commented on Jun 7, 2026

    ContributorAuthor

    This was completed in #31769

  4. added this to the v3.11.0 milestone on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL