Fixed memory management issue while closing native Win32 window #1126
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Intercepting window close in JS leads to wrong memory management when native window is closed (possibly invalid window handle).
I've seen it in debug-builds within VS2019, but it could possibly happen on release.
These are the steps to describe what happens:
windowClose
event, does its own tasks and then callsapp.exit()
app.exit()
callswindow::_close(code);
window::_close()
function contains the misbehavior:The marked line contains "non blocking" code, since it only calls
PostThreadMessage()
, so it returns immediately.The
delete nativeWindow;
line is so executed immediately (before the Window message posted by PostThreadMessage has been handled) and clearsnativeWindow
instance data.Once the message is handled,
DestroyWindow(m_window)
is executed but the value ofm_window
field could be invalid because its owning class was already deleted!It actually works just because... luckily no other code had ever reused/overwritten the same memory block previously used by nativeWindow..
Changes proposed
We need to ensure that
terminate()
won't return (and sodelete
won't be called) till native window has been destroyed.To do this I've used an event to synchronize the two threads: