Skip to content

Commit ff41391

Browse files
committed
Fix potential race in Metal on exit
1 parent f7ab0b9 commit ff41391

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

Diff for: hiro/cocoa/widget/label.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ auto pLabel::setForegroundColor(SystemColor color) -> void {
161161
}
162162

163163
auto pLabel::setText(const string& text) -> void {
164-
dispatch_async(dispatch_get_main_queue(), ^{
165-
[cocoaView setNeedsDisplay:YES];
166-
});
164+
[cocoaView setNeedsDisplay:YES];
167165
}
168166

169167
}

Diff for: ruby/video/metal/metal.cpp

+32-26
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,11 @@ struct VideoMetal : VideoDriver, Metal {
421421

422422
id<CAMetalDrawable> drawable = view.currentDrawable;
423423

424+
__block VideoMetal &strongSelf = self;
425+
424426
if (@available(macOS 10.15.4, *)) {
425427
[drawable addPresentedHandler:^(id<MTLDrawable> drawable) {
426-
self.drawableWasPresented(drawable);
428+
strongSelf.drawableWasPresented(drawable);
427429
depth--;
428430
}];
429431
}
@@ -599,31 +601,35 @@ struct VideoMetal : VideoDriver, Metal {
599601
}
600602

601603
auto terminate() -> void {
602-
_ready = false;
603-
604-
_commandQueue = nullptr;
605-
_library = nullptr;
606-
607-
_vertexBuffer = nullptr;
608-
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
609-
_sourceTextures[i] = nullptr;
610-
}
611-
_mtlVertexDescriptor = nullptr;
612-
613-
_renderToTextureRenderPassDescriptor = nullptr;
614-
_renderTargetTexture = nullptr;
615-
_renderToTextureRenderPipeline = nullptr;
616-
617-
_drawableRenderPipeline = nullptr;
618-
619-
if (_filterChain) {
620-
_libra.mtl_filter_chain_free(&_filterChain);
621-
}
622-
_device = nullptr;
623-
624-
if (view) {
625-
[view removeFromSuperview];
626-
view = nil;
604+
if(_renderQueue) {
605+
dispatch_sync(_renderQueue, ^{
606+
_ready = false;
607+
608+
_commandQueue = nullptr;
609+
_library = nullptr;
610+
611+
_vertexBuffer = nullptr;
612+
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
613+
_sourceTextures[i] = nullptr;
614+
}
615+
_mtlVertexDescriptor = nullptr;
616+
617+
_renderToTextureRenderPassDescriptor = nullptr;
618+
_renderTargetTexture = nullptr;
619+
_renderToTextureRenderPipeline = nullptr;
620+
621+
_drawableRenderPipeline = nullptr;
622+
623+
if (_filterChain) {
624+
_libra.mtl_filter_chain_free(&_filterChain);
625+
}
626+
_device = nullptr;
627+
628+
if (view) {
629+
[view removeFromSuperview];
630+
view = nil;
631+
}
632+
});
627633
}
628634
}
629635

0 commit comments

Comments
 (0)