Skip to content

Commit acd38ef

Browse files
authored
metal: Handle null returned from MTLCreateSystemDefaultDevice() (ggml-org#11441)
This fixes segmentation fault error when running tests when no metal devices are available (for example, when not linked with Core Graphics framework or otherwise).
1 parent caf773f commit acd38ef

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

ggml/src/ggml-metal/ggml-metal.m

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464

6565
if (ctx->mtl_device == nil) {
6666
ctx->mtl_device = MTLCreateSystemDefaultDevice();
67+
}
6768

69+
if (ctx->mtl_device) {
6870
ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
6971
ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
7072

@@ -99,8 +101,10 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
99101
ctx->mtl_device_ref_count--;
100102

101103
if (ctx->mtl_device_ref_count == 0) {
102-
[ctx->mtl_device release];
103-
ctx->mtl_device = nil;
104+
if (ctx->mtl_device) {
105+
[ctx->mtl_device release];
106+
ctx->mtl_device = nil;
107+
}
104108
}
105109
}
106110

0 commit comments

Comments
 (0)