metal : fix memory leaks due to missing autoreleasepools (#27758)

This commit is contained in:
Niklas Wenzel
2026-08-27 12:53:08 +03:00
committed by GitHub
parent c5fc7e3488
commit 915dc6d38c
2 changed files with 296 additions and 290 deletions
+2
View File
@@ -84,6 +84,7 @@ struct ggml_metal {
ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) { ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) {
GGML_LOG_INFO("%s: allocating\n", __func__); GGML_LOG_INFO("%s: allocating\n", __func__);
@autoreleasepool {
#if TARGET_OS_OSX && !GGML_METAL_NDEBUG #if TARGET_OS_OSX && !GGML_METAL_NDEBUG
// Show all the Metal device instances in the system // Show all the Metal device instances in the system
NSArray * devices = MTLCopyAllDevices(); NSArray * devices = MTLCopyAllDevices();
@@ -184,6 +185,7 @@ ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) {
res->pipelines_ext = ggml_metal_pipelines_init(); res->pipelines_ext = ggml_metal_pipelines_init();
return res; return res;
}
} }
void ggml_metal_free(ggml_metal_t ctx) { void ggml_metal_free(ggml_metal_t ctx) {
+4
View File
@@ -778,7 +778,9 @@ void ggml_metal_encoder_free(ggml_metal_encoder_t encoder) {
} }
void ggml_metal_encoder_debug_group_push(ggml_metal_encoder_t encoder, const char * name) { void ggml_metal_encoder_debug_group_push(ggml_metal_encoder_t encoder, const char * name) {
@autoreleasepool {
[encoder->obj pushDebugGroup:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]]; [encoder->obj pushDebugGroup:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];
}
} }
void ggml_metal_encoder_debug_group_pop (ggml_metal_encoder_t encoder) { void ggml_metal_encoder_debug_group_pop (ggml_metal_encoder_t encoder) {
@@ -1023,6 +1025,7 @@ ggml_metal_device_t ggml_metal_device_init(int device, int n_devices) {
assert(dev != NULL); assert(dev != NULL);
@autoreleasepool {
if (dev->mtl_device == nil) { if (dev->mtl_device == nil) {
dev->mtl_device = MTLCreateSystemDefaultDevice(); dev->mtl_device = MTLCreateSystemDefaultDevice();
@@ -1268,6 +1271,7 @@ ggml_metal_device_t ggml_metal_device_init(int device, int n_devices) {
#endif #endif
} }
} }
}
return dev; return dev;
} }