Fixes two critical warnings when closing a window with the close button. (cherry picked from commit c735479b)