Defer Cocoa window destuction post-iteration

This commit is contained in:
Jack Andersen 2016-01-09 22:01:09 -10:00
parent 6afe4eaff7
commit 5550909f7e
2 changed files with 20 additions and 15 deletions

View File

@ -116,8 +116,10 @@ public:
m_clientReturn = m_callback.appMain(this);
/* Cleanup here */
std::vector<std::unique_ptr<IWindow>> toDelete;
toDelete.reserve(m_windows.size());
for (auto& window : m_windows)
delete window.second;
toDelete.emplace_back(window.second);
});
/* Already in Cocoa's event loop; return now */

View File

@ -1222,34 +1222,31 @@ class WindowCocoa : public IWindow
WindowCocoaInternal* m_nsWindow;
GraphicsContextCocoa* m_gfxCtx;
EMouseCursor m_cursor = EMouseCursor::None;
bool m_closed = false;
public:
WindowCocoa(const std::string& title, NSOpenGLContext* lastGLCtx, MetalContext* metalCtx)
{
dispatch_sync(dispatch_get_main_queue(),
^{
m_nsWindow = [[WindowCocoaInternal alloc] initWithBooWindow:this title:title];
m_nsWindow = [[WindowCocoaInternal alloc] initWithBooWindow:this title:title];
#if BOO_HAS_METAL
if (metalCtx->m_dev)
m_gfxCtx = static_cast<GraphicsContextCocoa*>(_GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI::Metal, this, metalCtx));
else
if (metalCtx->m_dev)
m_gfxCtx = static_cast<GraphicsContextCocoa*>(_GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI::Metal, this, metalCtx));
else
#endif
m_gfxCtx = static_cast<GraphicsContextCocoa*>(_GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI::OpenGL3_3, this, lastGLCtx));
m_gfxCtx->initializeContext();
});
m_gfxCtx = static_cast<GraphicsContextCocoa*>(_GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI::OpenGL3_3, this, lastGLCtx));
m_gfxCtx->initializeContext();
}
void _clearWindow()
{
/* Caller consumes reference on its own */
(void)(__bridge_retained void*)m_nsWindow;
m_nsWindow = nullptr;
m_closed = true;
}
~WindowCocoa()
{
[m_nsWindow orderOut:nil];
if (!m_closed)
[m_nsWindow orderOut:nil];
delete m_gfxCtx;
APP->_deletedWindow(this);
}
@ -1494,7 +1491,12 @@ public:
IWindow* _WindowCocoaNew(const SystemString& title, NSOpenGLContext* lastGLCtx, MetalContext* metalCtx)
{
return new WindowCocoa(title, lastGLCtx, metalCtx);
__block IWindow* window = nullptr;
dispatch_sync(dispatch_get_main_queue(),
^{
window = new WindowCocoa(title, lastGLCtx, metalCtx);
});
return window;
}
}
@ -1509,6 +1511,7 @@ IWindow* _WindowCocoaNew(const SystemString& title, NSOpenGLContext* lastGLCtx,
NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:YES];
self.releasedWhenClosed = NO;
self.title = [NSString stringWithUTF8String:title.c_str()];
booWindow = bw;
return self;