Shared OpenGL context leads to crash moving window between screens

We are developing a graphics app that uses OpenGL extensively. We've developed a crash issue since we switched to CALayer-based OpenGL rendering per Apple sample code CALayerEssentials. When certain of our OpenGL layer-containing windows are moved between two screens, the app crashes (on Catalina). I replicated the issue in a pared-down, updated version of CALayerEssentials. In our version, because we will have multiple OpenGL views sharing resources, we use a shared context.

Is there something we can do differently to avoid the crash while still sharing OpenGL resources?

//		You would typically override this method if you needed to specify a share context to share OpenGL resources.
//		This is also an ideal location to do any initialization that is necessary for the context returned
-(CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
{
#if 0
// ORIGINAL SAMPLE CODE
	// Default handling is fine for this demonstration.
	CGLContextObj superContext = [super copyCGLContextForPixelFormat:pixelFormat];
	return superContext;
#else
// OUR VERSION TO SHARE OPENGL RESOURCES
// THIS LEADS TO CRASH WHEN WINDOW IS MOVED BETWEEN SCREENS
	if (!_dfContext)
	{
		_dfContext = [super copyCGLContextForPixelFormat:pixelFormat];
	}

	return _dfContext;
#endif
}

Our pared-down and updated version of CALayerEssentials source may be downloaded here.

Here's the backtrace:


Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   GLEngine                      	0x00007fff3eb701a2 gleLookupHashObject + 13
1   GLEngine                      	0x00007fff3eb6fe3e gleBindTexture + 59
2   GLEngine                      	0x00007fff3eb6fdc6 glBindTexture_Exec + 405
3   com.apple.QuartzCore          	0x00007fff3fbd8a0c CA::(anonymous namespace)::IOSurface::framebuffer() + 84
4   com.apple.QuartzCore          	0x00007fff3fbd8907 CA::(anonymous namespace)::IOSurface::attach() + 83
5   com.apple.QuartzCore          	0x00007fff3fbd8461 CAOpenGLLayerDraw(CAOpenGLLayer*, double, CVTimeStamp const*, unsigned int) + 1868
6   com.apple.QuartzCore          	0x00007fff3fbd7b3a -[CAOpenGLLayer _display] + 580
7   com.apple.QuartzCore          	0x00007fff3fb3bcad CA::Layer::display_if_needed(CA::Transaction*) + 757
8   com.apple.QuartzCore          	0x00007fff3fb19fca CA::Context::commit_transaction(CA::Transaction*, double) + 334
9   com.apple.QuartzCore          	0x00007fff3fb18bb4 CA::Transaction::commit() + 644
10  com.apple.AppKit              	0x00007fff315f42f1 __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 266
11  com.apple.AppKit              	0x00007fff31d12c20 ___NSRunLoopObserverCreateWithHandler_block_invoke + 41
12  com.apple.CoreFoundation      	0x00007fff341f17bc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
13  com.apple.CoreFoundation      	0x00007fff341f16ec __CFRunLoopDoObservers + 457
14  com.apple.CoreFoundation      	0x00007fff341f0c84 __CFRunLoopRun + 884
15  com.apple.CoreFoundation      	0x00007fff341f02b3 CFRunLoopRunSpecific + 466
16  com.apple.HIToolbox           	0x00007fff32e0baad RunCurrentEventLoopInMode + 292

Shared OpenGL context leads to crash moving window between screens
 
 
Q