While running our app developed on Windows 10 and closing the gui window with the "X" button we get a Microsoft Visual C++ Runtime Library Error (see attachment).
When running in debug mode an exception is triggered in the QApplication::exec() method.

MicrosoftError.PNG

This is our main function:

Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication app(argc, argv);
  4. GeDetektEventFilter filter;
  5. app.installNativeEventFilter(&filter);
  6.  
  7. std::unique_ptr<Sentinel>& sentinel = Sentinel::getInstance();
  8. sentinel->connectThreads();
  9. sentinel->createAppFiles();
  10. sentinel->startQmlEngine();
  11.  
  12. sentinel->startHardware();
  13.  
  14. manager::connections::connectAll();
  15.  
  16. //Uncomment this to deactivate tests (not recommended)
  17. ::testing::InitGoogleTest(&argc, argv);
  18. RUN_ALL_TESTS();
  19.  
  20. return qApp->exec();
  21. }
To copy to clipboard, switch view to plain text mode 

Our Gui is created with QtQuick and qml.

Qt Code:
  1. import QtQuick 2.2
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 1.4
  4. import QtQuick.Layouts 1.12
  5.  
  6. import DisplayInformation 1.0
  7.  
  8. Window
  9. {
  10. id: mainWindow
  11.  
  12. visible: true
  13. width: displayInformation.WINDOW_SIZE.width + displayInformation.SIDE_BAR_WIDTH
  14. height: displayInformation.WINDOW_SIZE.height
  15. title: qsTr("Sentinel")
  16.  
  17. maximumHeight: height
  18. maximumWidth: width
  19. minimumHeight: height
  20. minimumWidth: width
  21.  
  22. //Old Qt Bug Displays error here although functional
  23. onClosing:
  24. {
  25. sentinel.stopComClient();
  26. }
  27.  
  28. Rectangle
  29. {
  30. id: mainArea
  31. Keys.onPressed:
  32. {
  33. if (event.key === Qt.Key_F9)
  34. {
  35. sidebarLoader.source = ""
  36. qmlEngine.clearCache()
  37. sidebarLoader.source = "Sidebar.qml"
  38.  
  39. imageDisplayLoader.source = ""
  40. qmlEngine.clearCache()
  41. imageDisplayLoader.source = "ImageDisplay.qml"
  42.  
  43. if (mainArea.logOpen === true)
  44. {
  45. mainArea.logWindow.close()
  46. mainArea.logComponent.source = ""
  47. qmlEngine.clearCache()
  48. mainArea.logComponent.source = "LogWindow.qml"
  49. }
  50.  
  51. console.log("Sources Reloaded");
  52.  
  53. event.accepted = true;
  54. }
  55.  
  56. if (event.key === Qt.Key_L && logOpen === false)
  57. {
  58. sentinel.startHardware()
  59.  
  60. console.log("Log Window")
  61. logComponent = Qt.createComponent("LogWindow.qml")
  62. logWindow = logComponent.createObject(mainWindow)
  63.  
  64. logWindow.show()
  65. logOpen = true
  66. }
  67. }
  68.  
  69. Connections
  70. {
  71. target: mainArea.logWindow
  72. onClosing:
  73. {
  74. console.log("Log Window closed")
  75. mainArea.logOpen = false
  76. }
  77. }
  78.  
  79. property bool scanAvailable: false
  80. property bool scanStopped: true
  81. property bool logOpen: false
  82. property Window logWindow
  83. property Component logComponent
  84. }
  85.  
  86. RowLayout
  87. {
  88. id: rowLayout
  89. anchors.fill: parent
  90.  
  91. Loader
  92. {
  93. id: imageDisplayLoader
  94. source: "ImageDisplay.qml"
  95. }
  96.  
  97. Loader
  98. {
  99. id: sidebarLoader
  100. Layout.fillHeight: true
  101. Layout.fillWidth: true
  102. source: "Sidebar.qml"
  103. }
  104. }
  105. }
To copy to clipboard, switch view to plain text mode 

The qml files are loaded in following function over an environment variable:

Qt Code:
  1. void Sentinel::startQmlEngine()
  2. {
  3. qmlRegisterType<gui::DisplayInformation>("DisplayInformation", 1, 0, "DisplayInformation");
  4. qmlRegisterType<gui::ImageItem>("ImageItem",1,0,"ImageItem");
  5. qmlRegisterType<gui::ImageProvider>("ImageProvider", 1, 0, "ImageProvider");
  6.  
  7. m_qmlEngine.rootContext()->setContextProperty("displayInformation",
  8. &m_imageProvider.getDisplayInformation());
  9.  
  10. m_qmlEngine.rootContext()->setContextProperty("sentinel", this);
  11. m_qmlEngine.rootContext()->setContextProperty("imageProvider", &m_imageProvider);
  12. m_qmlEngine.rootContext()->setContextProperty("scanArea", modules::ScanArea::getInstance().get());
  13. m_qmlEngine.rootContext()->setContextProperty("comClient", modules::ComClient::getInstance().get());
  14. m_qmlEngine.rootContext()->setContextProperty("ptu", modules::PtuControl::getInstance().get());
  15. m_qmlEngine.rootContext()->setContextProperty("logOutput", helpers::LogOutput::getInstance().get());
  16. m_qmlEngine.rootContext()->setContextProperty("qmlEngine", &m_qmlEngine);
  17.  
  18. m_qmlEngine.load(qEnvironmentVariable("MAIN_QML"));
  19. assert(!m_qmlEngine.rootObjects().isEmpty());
  20.  
  21. //These are the connections interfacing module state to gui (image processing methods and qml)
  22. using namespace modules;
  23.  
  24. QObject::connect(&m_imageProvider,
  25. &gui::ImageProvider::backgroundChanged,
  26. this, &Sentinel::updateBackgroundS);
  27.  
  28. QObject::connect(ComClient::getInstance().get(),
  29. &ComClient::compoundsNameAndStateChanged,
  30. this, &Sentinel::updateCompoundsS);
  31.  
  32. QObject::connect(GigECamera::getInstance().get(),
  33. &GigECamera::newFrameAvailable,
  34. &m_imageProvider,
  35. &gui::ImageProvider::updateBackgroundImage);
  36.  
  37. QObject::connect(ToxelBufferExtractor::getInstance().get(),
  38. &ToxelBufferExtractor::updateToxelPixel,
  39. &m_imageProvider,
  40. &gui::ImageProvider::paintNewOverlayCircle);
  41. }
To copy to clipboard, switch view to plain text mode 

I've researched for quite a bit to find a solution for this and have seen there were some reported bugs refering to this error. However, I am not sure if and when these bugs have been fixed or even apply to our problem.
There might also be something missing in the way we create and quit the app as this is a very general application scenario for a Qt app and quite a disturbing behaviour.

Very grateful for any suggestions.