Avoid using invalid index in wxDisplayFactory under Mac

Partially work around currently unimplemented cache invalidation in
wxMac and do it on the fly if an invalid index is passed to
GetDisplay() to at least avoid crashing, even if this doesn't fully
solve the problem, e.g. we still can use stale information.

Closes #18607.
This commit is contained in:
Vadim Zeitlin 2019-12-04 19:22:07 +01:00
parent 7b36d72b44
commit 462e7b7732

View File

@ -28,10 +28,32 @@ public:
// get it from cache.
wxDisplayImpl* GetDisplay(unsigned n)
{
if ( m_impls.empty() )
// Normally, m_impls should be cleared if the number of displays in the
// system changes because InvalidateCache() must be called. However in
// some ports (e.g. Mac right now, see #18318), cache invalidation never
// happens, so we can end up with m_impls size being out of sync with
// the actual number of monitors. Compensate for this here by checking
// if the index is invalid and invalidating the cache at least in this
// case.
//
// Note that this is still incorrect because we continue using outdated
// information if the first monitor is disconnected, for example. The
// only real solution is to ensure that InvalidateCache() is called,
// but for now this at least avoids crashes when a new display is
// connected.
if ( n >= m_impls.size() )
{
// This strange two-step resize is done to clear all the existing
// elements: they may not be valid any longer if the number of
// displays has changed.
m_impls.resize(0);
m_impls.resize(GetCount());
}
else if ( m_impls[n] )
{
// Just return the existing display if we have it.
return m_impls[n];
}
m_impls[n] = CreateDisplay(n);
return m_impls[n];