From 1cb4f399069e81a4a19642ceaa4fa079333d08ff Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 31 Aug 2026 13:20:36 +0800 Subject: [PATCH] Ensure end-of-application object lifecycle is clean. --- .../{{ cookiecutter.class_name }}/main.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/{{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m b/{{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m index 9fd83eb..41ac8c7 100644 --- a/{{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m +++ b/{{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m @@ -334,9 +334,14 @@ int main(int argc, char *argv[]) { ret = -7; } @finally { - Py_Finalize(); + // Force a full GC pass while the interpreter is alive and a pool is + // in place, so Python drops any ObjC references and anything + // autoreleased lands in this pool. + PyGC_Collect(); } } + // Autorelease pool is drained; any Python -dealloc has run against a live interpreter. + Py_Finalize(); exit(ret); return ret;