Safeguards event handling during application shutdown

Introduces a flag to track if the application is currently in its destruction phase.
The `notify` method now checks this flag and, if set, delegates event processing directly to the base `QApplication::notify`.
This prevents custom event handlers from attempting to access member variables that may have already been destroyed or are in an invalid state, particularly when `close()` calls on main windows might trigger events during `RiaGuiApplication`'s destruction.
This commit is contained in:
Magne Sjaastad committed 2026-03-26 11:20:41 +01:00
1 parent bff9e18b20
commit 704c5f55f0
1 file changed
+43
@@ -186,11 +186,47 @@ RiaGuiApplication::RiaGuiApplication( int& argc, char** argv )
connect( this, SIGNAL( lastWindowClosed() ), SLOT( onLastWindowClosed() ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
namespace
{
// Helper to access the destruction flag used in notify
bool& getNotifyInDestructorFlag()
{
static bool inDestructor = false;
return inDestructor;
}
void setNotifyInDestructorFlag( bool value )
{
getNotifyInDestructorFlag() = value;
}
} // namespace
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaGuiApplication::~RiaGuiApplication()
{
// Set destruction flag to prevent notify from accessing member variables during destruction
// This must be done before any window deletion that might trigger events
setNotifyInDestructorFlag( true );
processEvents();
if ( m_mainWindow )
{
m_mainWindow->close();
}
if ( m_mainPlotWindow )
{
m_mainPlotWindow->close();
}
processEvents();
delete m_mainWindow.data();
m_mainWindow.clear();
@@ -1754,6 +1790,13 @@ bool RiaGuiApplication::notify( QObject* receiver, QEvent* event )
"unstable and will probably crash soon." );
}
// Safety guard: If we're being called during destruction, delegate directly to base class
// to avoid accessing potentially destroyed member variables
if ( getNotifyInDestructorFlag() )
{
return QApplication::notify( receiver, event );
}
bool done = false;
try
{