Fix crash when reparenting the focused window to another TLW

If the window stored as m_winLastFocused in one TLW was reparented to
another one and then destroyed, this pointer to it wasn't updated and
became dangling.

Fix this by using a safe weak reference instead of raw pointer for
m_winLastFocused. This ensures that it can never be used when it becomes
invalid.

Closes #17980.
This commit is contained in:
Vadim Zeitlin
2017-10-27 17:49:13 +02:00
parent c2d11dc275
commit 31d51186e2
3 changed files with 7 additions and 4 deletions

View File

@@ -214,6 +214,7 @@ wxMSW:
- Fix updating radio groups when non-radio item is inserted to wxMenu. - Fix updating radio groups when non-radio item is inserted to wxMenu.
- Fix autoselecting the contents of wxTextCtrl with wxWANTS_CHARS style. - Fix autoselecting the contents of wxTextCtrl with wxWANTS_CHARS style.
- Implement SetIcon(), SetPosition(), GetPosition() for native wxProgressDialog. - Implement SetIcon(), SetPosition(), GetPosition() for native wxProgressDialog.
- Fix crash when reparenting the currently focused window to another TLW.
wxOSX: wxOSX:

View File

@@ -11,6 +11,8 @@
#ifndef _WX_MSW_TOPLEVEL_H_ #ifndef _WX_MSW_TOPLEVEL_H_
#define _WX_MSW_TOPLEVEL_H_ #define _WX_MSW_TOPLEVEL_H_
#include "wx/weakref.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxTopLevelWindowMSW // wxTopLevelWindowMSW
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -176,7 +178,7 @@ protected:
// The last focused child: we remember it when we're deactivated and // The last focused child: we remember it when we're deactivated and
// restore focus to it when we're activated (this is done here) or restored // restore focus to it when we're activated (this is done here) or restored
// from iconic state (done by wxFrame). // from iconic state (done by wxFrame).
wxWindow *m_winLastFocused; wxWindowRef m_winLastFocused;
private: private:

View File

@@ -102,8 +102,6 @@ void wxTopLevelWindowMSW::Init()
m_fsIsMaximized = false; m_fsIsMaximized = false;
m_fsIsShowing = false; m_fsIsShowing = false;
m_winLastFocused = NULL;
m_menuSystem = NULL; m_menuSystem = NULL;
} }
@@ -1195,7 +1193,9 @@ void wxTopLevelWindowMSW::DoRestoreLastFocus()
parent = this; parent = this;
} }
wxSetFocusToChild(parent, &m_winLastFocused); wxWindow* winPtr = m_winLastFocused;
wxSetFocusToChild(parent, &winPtr);
m_winLastFocused = winPtr;
} }
void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event) void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)