From e5103ae6ba31d7d15e6e8295171fa2bc6411f834 Mon Sep 17 00:00:00 2001 From: kevingpqi123 Date: Wed, 22 Jan 2025 19:07:03 +0800 Subject: [PATCH] Add timely release of autorelease objects on the macOS platform. (#440) --- src/gpu/opengl/cgl/CGLDevice.mm | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/gpu/opengl/cgl/CGLDevice.mm b/src/gpu/opengl/cgl/CGLDevice.mm index 76a7f801..148e505c 100644 --- a/src/gpu/opengl/cgl/CGLDevice.mm +++ b/src/gpu/opengl/cgl/CGLDevice.mm @@ -68,20 +68,22 @@ if (glDevice) { return std::static_pointer_cast(glDevice); } - auto oldCGLContext = CGLGetCurrentContext(); - if (oldCGLContext != cglContext) { - CGLSetCurrentContext(cglContext); - if (CGLGetCurrentContext() != cglContext) { - return nullptr; + @autoreleasepool { + auto oldCGLContext = CGLGetCurrentContext(); + if (oldCGLContext != cglContext) { + CGLSetCurrentContext(cglContext); + if (CGLGetCurrentContext() != cglContext) { + return nullptr; + } } + auto device = std::shared_ptr(new CGLDevice(cglContext)); + device->externallyOwned = externallyOwned; + device->weakThis = device; + if (oldCGLContext != cglContext) { + CGLSetCurrentContext(oldCGLContext); + } + return device; } - auto device = std::shared_ptr(new CGLDevice(cglContext)); - device->externallyOwned = externallyOwned; - device->weakThis = device; - if (oldCGLContext != cglContext) { - CGLSetCurrentContext(oldCGLContext); - } - return device; } CGLDevice::CGLDevice(CGLContextObj cglContext) : GLDevice(cglContext) { @@ -119,10 +121,12 @@ } bool CGLDevice::onMakeCurrent() { - oldContext = CGLGetCurrentContext(); - CGLRetainContext(oldContext); - [glContext makeCurrentContext]; - return [NSOpenGLContext currentContext] == glContext; + @autoreleasepool { + oldContext = CGLGetCurrentContext(); + CGLRetainContext(oldContext); + [glContext makeCurrentContext]; + return [NSOpenGLContext currentContext] == glContext; + } } void CGLDevice::onClearCurrent() {