Skip to content

Commit b4b576f

Browse files
author
Martin Fox
committed
8322215: [win] OS events that close the stage can cause Glass to reference freed memory
Reviewed-by: kcr Backport-of: c1c52e5
1 parent 9587f51 commit b4b576f

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

modules/javafx.graphics/src/main/native-glass/win/BaseWnd.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ BaseWnd::BaseWnd(HWND ancestor) :
3939
m_ancestor(ancestor),
4040
m_wndClassAtom(0),
4141
m_isCommonDialogOwner(false),
42-
m_hCursor(NULL)
42+
m_hCursor(NULL),
43+
m_messageCount(0),
44+
m_isDead(false)
4345
{
4446

4547
}
@@ -159,8 +161,9 @@ LRESULT CALLBACK BaseWnd::StaticWindowProc(HWND hWnd, UINT msg, WPARAM wParam, L
159161
pThis = (BaseWnd *)::GetProp(hWnd, szBaseWndProp);
160162
}
161163
if (pThis != NULL) {
164+
pThis->BeginMessageProcessing(msg);
162165
LRESULT result = pThis->WindowProc(msg, wParam, lParam);
163-
if (msg == WM_NCDESTROY) {
166+
if (pThis->EndMessageProcessing()) {
164167
::RemoveProp(hWnd, szBaseWndProp);
165168
delete pThis;
166169
}
@@ -169,6 +172,23 @@ LRESULT CALLBACK BaseWnd::StaticWindowProc(HWND hWnd, UINT msg, WPARAM wParam, L
169172
return ::DefWindowProc(hWnd, msg, wParam, lParam);
170173
}
171174

175+
/*non-static*/
176+
void BaseWnd::BeginMessageProcessing(UINT msg)
177+
{
178+
if (msg == WM_NCDESTROY) {
179+
m_isDead = true;
180+
}
181+
m_messageCount += 1;
182+
}
183+
184+
bool BaseWnd::EndMessageProcessing()
185+
{
186+
if (m_messageCount > 0) {
187+
m_messageCount -= 1;
188+
}
189+
return m_isDead && (m_messageCount == 0);
190+
}
191+
172192
/*virtual*/
173193
MessageResult BaseWnd::CommonWindowProc(UINT msg, WPARAM wParam, LPARAM lParam)
174194
{

modules/javafx.graphics/src/main/native-glass/win/BaseWnd.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class BaseWnd {
6767

6868
void SetCursor(HCURSOR cursor);
6969

70+
// Begin processing a message.
71+
void BeginMessageProcessing(UINT msg);
72+
// End processing a message. Returns 'true' if the BaseWnd should be
73+
// deleted.
74+
bool EndMessageProcessing();
75+
7076
private:
7177
HWND m_hWnd;
7278
static LRESULT CALLBACK StaticWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
@@ -78,6 +84,10 @@ class BaseWnd {
7884
ATOM m_wndClassAtom;
7985
bool m_isCommonDialogOwner;
8086
HCURSOR m_hCursor;
87+
88+
LONG m_messageCount;
89+
bool m_isDead;
90+
8191
protected:
8292
virtual LRESULT WindowProc(UINT msg, WPARAM wParam, LPARAM lParam) = 0;
8393
virtual MessageResult CommonWindowProc(UINT msg, WPARAM wParam, LPARAM lParam);

0 commit comments

Comments
 (0)