Skip to content

Commit c9ea801

Browse files
ptsochantarisjordankanter
authored andcommitted
metal : fix memory leak, dangling pointer and unused autorel (ggml-org#5007)
* Metal memory: Small memory leak on init, dangling pointer, and unused autorelease pool in graph compute * SPM header potential fix * Reverting symlinks
1 parent 7b73e9d commit c9ea801

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

ggml-metal.m

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,19 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
238238
static struct ggml_metal_context * ggml_metal_init(int n_cb) {
239239
GGML_METAL_LOG_INFO("%s: allocating\n", __func__);
240240

241-
id<MTLDevice> device;
242-
NSString * s;
243-
244-
#if TARGET_OS_OSX
241+
#if TARGET_OS_OSX && !GGML_METAL_NDEBUG
245242
// Show all the Metal device instances in the system
246243
NSArray * devices = MTLCopyAllDevices();
247-
for (device in devices) {
248-
s = [device name];
244+
for (id<MTLDevice> device in devices) {
245+
NSString * s = [device name];
249246
GGML_METAL_LOG_INFO("%s: found device: %s\n", __func__, [s UTF8String]);
250247
}
248+
[devices release]; // since it was created by a *Copy* C method
251249
#endif
252250

253251
// Pick and show default Metal device
254-
device = MTLCreateSystemDefaultDevice();
255-
s = [device name];
252+
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
253+
NSString * s = [device name];
256254
GGML_METAL_LOG_INFO("%s: picking default device: %s\n", __func__, [s UTF8String]);
257255

258256
// Configure context
@@ -712,7 +710,6 @@ static bool ggml_metal_supports_op(const struct ggml_metal_context * ctx, const
712710
static bool ggml_metal_graph_compute(
713711
struct ggml_metal_context * ctx,
714712
struct ggml_cgraph * gf) {
715-
@autoreleasepool {
716713

717714
MTLComputePassDescriptor * edesc = MTLComputePassDescriptor.computePassDescriptor;
718715
edesc.dispatchType = MTLDispatchTypeSerial;
@@ -2255,7 +2252,6 @@ static bool ggml_metal_graph_compute(
22552252
}
22562253

22572254
return true;
2258-
}
22592255
}
22602256

22612257
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)