Friday, April 9, 2021

Recent Questions - Stack Overflow

Recent Questions - Stack Overflow

Recent Questions - Stack Overflow


Handling memory exhausted in Windows Qt app

Posted: 09 Apr 2021 08:54 AM PDT

My application can use a lot of memory and I want to be able to handle running out of memory gracefully. So I do this:

bool Application::notify( QObject* receiver, QEvent* event )  {      bool done = true;      try      {          done = QApplication::notify( receiver, event );      }      catch ( std::bad_alloc& ex )      {          releaseMemory();          qWarning() << "bad_alloc exception: " << ex.what();          m_outOfMemoryDlg->exec();          exit( 1 );      }      catch ( const std::exception& ex )      {          releaseMemory();          qWarning() << "exception: " << ex.what();          criticalMessage( nullptr, QString( "%1 has to close due to an exception. Please contact support." ).arg( APP_NAME ) );          exit( 2 );      }      catch ( ... )      {          releaseMemory();          qWarning() << "exception";          criticalMessage( nullptr, QString( "%1 has to close. Please contact support." ).arg( APP_NAME ) );          exit( 3 );      }      return done;  }  

Where m_outOfMemoryDlg is a QDialog with a helpful message I create and hide at startup. This all works fine on macOS. I get a std::bad_alloc exception, the QDialog appears and the program shuts gracefully. But on Windows the whole OS just locks. The screen is frozen and I have to power off and back on to make it responsive.

In my .pro file I added:

CONFIG += exceptions    win32 {      QMAKE_CXXFLAGS_EXCEPTIONS_ON = /EHa      QMAKE_CXXFLAGS_STL_ON = /EHa  }  

I also added:

#ifdef Q_OS_WIN  #include <new.h>  LONG WINAPI  exceptionFilter( struct _EXCEPTION_POINTERS* exceptionInfo )  {      if ( exceptionInfo )      {          EXCEPTION_RECORD* er = exceptionInfo->ExceptionRecord;          if ( er )          {              qWarning() << "Windows exception: " << er->ExceptionCode;          }      }      exit( 4 );      return EXCEPTION_EXECUTE_HANDLER;  }    int outOfMemory( size_t )  {      qWarning() << "Out of memory";      exit( 5 );      return 0;  }  

No comments:

Post a Comment