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?
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.
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.
Activity
QuLogic commented on May 27, 2026
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.
iccir commented on May 27, 2026
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.
iccir commented on Jun 7, 2026
This was completed in #31769