Skip to content

metal : fix more leaks due to missing autoreleasepools - #27882

Closed
nikwen wants to merge 1 commit into
ggml-org:masterfrom
nikwen:nikwen/autorelease-pools
Closed

metal : fix more leaks due to missing autoreleasepools#27882
nikwen wants to merge 1 commit into
ggml-org:masterfrom
nikwen:nikwen/autorelease-pools

Conversation

@nikwen

@nikwen nikwen commented Aug 28, 2026

Copy link
Copy Markdown
Member

Overview

Continuation of #27758.

Fixes various messages of the following form when running with OBJC_DEBUG_MISSING_POOLS=YES:

objc[97197]: MISSING POOLS: (0x17025b000) Object 0x73a840000 of class AGXG16SDevice autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

Additional information

Backtraces of the fixed locations when running in lldb with breakpoint set -n objc_autoreleaseNoPool:

Backtraces on exit

objc[8378]: MISSING POOLS: (0x17085f000) Object 0x84a888000 of class AGXG16SDevice autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
Process 8378 stopped
* thread #21, stop reason = breakpoint 1.1
    frame #0: 0x000000018f848a38 libobjc.A.dylib`_objc_warn_deprecated
libobjc.A.dylib`_objc_warn_deprecated:
->  0x18f848a38 <+0>:  ret    
    0x18f848a3c <+4>:  udf    #0x0
    0x18f848a40 <+8>:  udf    #0x0
    0x18f848a44 <+12>: udf    #0x0
Target 0: (llama-cli) stopped.
(lldb) bt
* thread #21, stop reason = breakpoint 1.1
  * frame #0: 0x000000018f848a38 libobjc.A.dylib`_objc_warn_deprecated
    frame #1: 0x000000018f83fff8 libobjc.A.dylib`AutoreleasePoolPage::autoreleaseNoPage(objc_object*) + 260
    frame #2: 0x000000018f842218 libobjc.A.dylib`objc_autorelease + 184
    frame #3: 0x00000001b535ce74 IOGPU`-[IOGPUMetalResource dealloc] + 228
    frame #4: 0x00000001b5348670 IOGPU`-[IOGPUMetalBuffer dealloc] + 320
    frame #5: 0x000000010e9fd078 AGXMetalG16X`-[AGXBuffer dealloc] + 96
    frame #6: 0x000000010ea7dff4 AGXMetalG16X`-[AGXG16XFamilyBuffer dealloc] + 76
    frame #7: 0x00000001b53538e8 IOGPU`-[IOGPUMetalResidencySet commit] + 652
    frame #8: 0x0000000100faca50 libggml-metal.0.dylib`ggml_metal_buffer_free + 152
    frame #9: 0x000000010065b8a0 libggml-base.0.dylib`ggml_backend_buffer_free + 32
    frame #10: 0x0000000101756c50 libllama.0.dylib`llama_kv_cache::~llama_kv_cache() + 324
    frame #11: 0x0000000101755fc4 libllama.0.dylib`llama_kv_cache::~llama_kv_cache() + 12
    frame #12: 0x000000010175b0a4 libllama.0.dylib`llama_kv_cache_iswa::~llama_kv_cache_iswa() + 52
    frame #13: 0x0000000101708748 libllama.0.dylib`llama_context::~llama_context() + 636
    frame #14: 0x0000000101711520 libllama.0.dylib`llama_free + 16
    frame #15: 0x0000000101cbba8c libllama-common.0.dylib`common_init_result::impl::~impl() + 188
    frame #16: 0x0000000101cb9078 libllama-common.0.dylib`common_init_result::~common_init_result() + 36
    frame #17: 0x0000000100850b20 libllama-server-impl.dylib`server_context_impl::destroy() + 76
    frame #18: 0x0000000100850d40 libllama-server-impl.dylib`server_context_impl::~server_context_impl() + 40
    frame #19: 0x000000010082d2cc libllama-server-impl.dylib`server_context::~server_context() + 36
    frame #20: 0x0000000100792130 libllama-server-impl.dylib`llama_server(common_params&, int, char**) + 22752
    frame #21: 0x0000000100068208 libllama-cli-impl.dylib`void* std::__1::__thread_proxy[abi:nqe210106]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, cli_server::start(common_params&)::'lambda'()>>(void*) + 64
    frame #22: 0x000000018fca7c58 libsystem_pthread.dylib`_pthread_start + 136
objc[9965]: MISSING POOLS: (0x1fc6e6180) Object 0xbd4858000 of class AGXG16SDevice autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
Process 9965 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x000000018f848a38 libobjc.A.dylib`_objc_warn_deprecated
libobjc.A.dylib`_objc_warn_deprecated:
->  0x18f848a38 <+0>:  ret    
    0x18f848a3c <+4>:  udf    #0x0
    0x18f848a40 <+8>:  udf    #0x0
    0x18f848a44 <+12>: udf    #0x0
Target 0: (llama-cli) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x000000018f848a38 libobjc.A.dylib`_objc_warn_deprecated
    frame #1: 0x000000018f83fff8 libobjc.A.dylib`AutoreleasePoolPage::autoreleaseNoPage(objc_object*) + 260
    frame #2: 0x000000018f842218 libobjc.A.dylib`objc_autorelease + 184
    frame #3: 0x00000001b535ce74 IOGPU`-[IOGPUMetalResource dealloc] + 228
    frame #4: 0x00000001b535db64 IOGPU`-[IOGPUMetalResourcePool purgeWithLock] + 132
    frame #5: 0x00000001b535dc0c IOGPU`-[IOGPUMetalResourcePool purge] + 36
    frame #6: 0x00000001b534fb64 IOGPU`-[IOGPUMetalDevice _purgeDevice] + 156
    frame #7: 0x000000010eacdda4 AGXMetalG16X`-[AGXG16XFamilyDevice _purgeDevice] + 76
    frame #8: 0x000000019c342e84 Metal`-[_MTLCommandQueue dealloc] + 228
    frame #9: 0x00000001b534b688 IOGPU`-[IOGPUMetalCommandQueue dealloc] + 112
    frame #10: 0x000000010ea862bc AGXMetalG16X`-[AGXG16XFamilyCommandQueue dealloc] + 84
    frame #11: 0x0000000100fab6bc libggml-metal.0.dylib`ggml_metal_device_free + 48
    frame #12: 0x0000000100fad654 libggml-metal.0.dylib`std::__1::vector<std::__1::unique_ptr<ggml_metal_device, ggml_metal_device_deleter>, std::__1::allocator<std::__1::unique_ptr<ggml_metal_device, ggml_metal_device_deleter>>>::~vector[abi:nqe210106]() + 72
    frame #13: 0x000000018fb5c7dc libsystem_c.dylib`__cxa_finalize_ranges + 416
    frame #14: 0x000000018fb5c5dc libsystem_c.dylib`exit + 44
    frame #15: 0x000000018f8a42e4 libdyld.dylib`dyld4::LibSystemHelpers::exit(int) const + 20
    frame #16: 0x000000018f8dc5f0 dyld`dyld4::LibSystemHelpersWrapper::exit(int) const + 164
    frame #17: 0x000000018f8dc514 dyld`start + 7040

Backtrace on each generated token

objc[97197]: MISSING POOLS: (0x17025b000) Object 0x73a840000 of class AGXG16SDevice autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
Process 97197 stopped
* thread #9, queue = 'com.Metal.CompletionQueueDispatch', stop reason = breakpoint 1.1
    frame #0: 0x000000018f848a38 libobjc.A.dylib`_objc_warn_deprecated
libobjc.A.dylib`_objc_warn_deprecated:
->  0x18f848a38 <+0>:  ret    
    0x18f848a3c <+4>:  udf    #0x0
    0x18f848a40 <+8>:  udf    #0x0
    0x18f848a44 <+12>: udf    #0x0
Target 0: (llama-cli) stopped.
(lldb) bt
* thread #9, queue = 'com.Metal.CompletionQueueDispatch', stop reason = breakpoint 1.1
  * frame #0: 0x000000018f848a38 libobjc.A.dylib`_objc_warn_deprecated
    frame #1: 0x000000018f83fff8 libobjc.A.dylib`AutoreleasePoolPage::autoreleaseNoPage(objc_object*) + 260
    frame #2: 0x000000018f842218 libobjc.A.dylib`objc_autorelease + 184
    frame #3: 0x00000001b535ce74 IOGPU`-[IOGPUMetalResource dealloc] + 228
    frame #4: 0x00000001b5348670 IOGPU`-[IOGPUMetalBuffer dealloc] + 320
    frame #5: 0x000000010e9fd078 AGXMetalG16X`-[AGXBuffer dealloc] + 96
    frame #6: 0x000000010ea7dff4 AGXMetalG16X`-[AGXG16XFamilyBuffer dealloc] + 76
    frame #7: 0x000000019c4d2f2c Metal`MTLResourceListChunkFreeEntries(MTLResourceListChunk*) + 56
    frame #8: 0x000000019c33b52c Metal`-[MTLResourceList releaseAllObjectsAndReset] + 68
    frame #9: 0x00000001b53575d0 IOGPU`IOGPUMetalCommandBufferStorageReset + 40
    frame #10: 0x00000001b5357568 IOGPU`IOGPUMetalCommandBufferStorageDealloc + 76
    frame #11: 0x00000001b5349244 IOGPU`-[IOGPUMetalCommandBuffer didCompleteWithStartTime:endTime:error:] + 240
    frame #12: 0x000000019c33af18 Metal`-[_MTLCommandQueue commandBufferDidComplete:startTime:completionTime:error:] + 108
    frame #13: 0x00000001b535565c IOGPU`IOGPUNotificationQueueDispatchAvailableCompletionNotifications + 136
    frame #14: 0x00000001b535576c IOGPU`__IOGPUNotificationQueueSetDispatchQueue_block_invoke + 64
    frame #15: 0x000000018fb054f8 libdispatch.dylib`_dispatch_client_callout4 + 16
    frame #16: 0x000000018fb07e94 libdispatch.dylib`_dispatch_mach_msg_invoke + 480
    frame #17: 0x000000018faf3e98 libdispatch.dylib`_dispatch_lane_serial_drain + 332
    frame #18: 0x000000018fb08bec libdispatch.dylib`_dispatch_mach_invoke + 472
    frame #19: 0x000000018faf3e98 libdispatch.dylib`_dispatch_lane_serial_drain + 332
    frame #20: 0x000000018faf4b64 libdispatch.dylib`_dispatch_lane_invoke + 448
    frame #21: 0x000000018faf3e98 libdispatch.dylib`_dispatch_lane_serial_drain + 332
    frame #22: 0x000000018faf4b2c libdispatch.dylib`_dispatch_lane_invoke + 392
    frame #23: 0x000000018fafee34 libdispatch.dylib`_dispatch_root_queue_drain_deferred_wlh + 284
    frame #24: 0x000000018fafe734 libdispatch.dylib`_dispatch_workloop_worker_thread + 720
    frame #25: 0x000000018fca3ec0 libsystem_pthread.dylib`_pthread_wqthread + 292

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Fable 5 helped me debug and fix the leaks. Lots of manual work and verification, too.

@nikwen
nikwen requested a review from a team as a code owner August 28, 2026 14:01
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Aug 28, 2026
@nikwen

nikwen commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

Sorry, accidentally opened this from my fork. Will re-open from ggml-org/llama.cpp so that I can make this part of a PR stack.

@nikwen nikwen closed this Aug 28, 2026
@nikwen

nikwen commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

Re-opened as #27883.

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

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant