Do not destroy background thread before application end. v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 29 Sep 2018 13:26:33 +0200
branchv_0
changeset 16 b6f790137bb8
parent 15 547a7c5681a5
child 17 dad72beb3ebe
Do not destroy background thread before application end. Avoids "QThread: Destroyed while thread is still running" and SIGABRT / core dump. This error occured when the thread was still reading STDIN (no EOF yet) and the window was closed.
nbproject/configurations.xml
src/relpipe-out-chart.cpp
--- a/nbproject/configurations.xml	Sat Sep 29 12:58:04 2018 +0200
+++ b/nbproject/configurations.xml	Sat Sep 29 13:26:33 2018 +0200
@@ -139,6 +139,12 @@
           <incDir>
             <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
           </incDir>
+          <preprocessorList>
+            <Elem>QT_CHARTS_LIB</Elem>
+            <Elem>QT_CORE_LIB</Elem>
+            <Elem>QT_GUI_LIB</Elem>
+            <Elem>QT_WIDGETS_LIB</Elem>
+          </preprocessorList>
         </ccTool>
       </folder>
       <folder path="Modules">
@@ -186,6 +192,23 @@
       <item path="src/RelpipeChartMainWindow.cpp" ex="false" tool="1" flavor2="8">
         <ccTool flags="0">
           <incDir>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
+            <pElem>build/Debug/src</pElem>
+            <pElem>src</pElem>
+            <pElem>build/Debug/src/relpipe-out-chart_autogen/include</pElem>
+            <pElem>../relpipe-lib-reader.cpp/include</pElem>
+            <pElem>../relpipe-lib-cli.cpp/include</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
+            <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCharts</pElem>
+          </incDir>
+        </ccTool>
+      </item>
+      <item path="src/relpipe-out-chart.cpp" ex="false" tool="1" flavor2="8">
+        <ccTool flags="0">
+          <incDir>
             <pElem>build/Debug/src</pElem>
             <pElem>src</pElem>
             <pElem>build/Debug/src/relpipe-out-chart_autogen/include</pElem>
@@ -198,30 +221,6 @@
             <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
             <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCharts</pElem>
           </incDir>
-          <preprocessorList>
-            <Elem>QT_CHARTS_LIB</Elem>
-            <Elem>QT_CORE_LIB</Elem>
-            <Elem>QT_GUI_LIB</Elem>
-            <Elem>QT_WIDGETS_LIB</Elem>
-          </preprocessorList>
-        </ccTool>
-      </item>
-      <item path="src/relpipe-out-chart.cpp" ex="false" tool="1" flavor2="8">
-        <ccTool flags="1">
-          <incDir>
-            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
-            <pElem>../relpipe-lib-cli.cpp/include/relpipe/cli</pElem>
-            <pElem>../relpipe-lib-reader.cpp/include/relpipe/reader/handlers</pElem>
-            <pElem>src</pElem>
-            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
-            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
-            <pElem>../relpipe-lib-reader.cpp/include/relpipe/reader</pElem>
-            <pElem>build/Debug/src/relpipe-out-chart_autogen/include</pElem>
-            <pElem>../relpipe-lib-cli.cpp/include</pElem>
-            <pElem>../relpipe-lib-reader.cpp/include</pElem>
-            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
-            <pElem>build/Debug/src</pElem>
-          </incDir>
         </ccTool>
       </item>
     </conf>
--- a/src/relpipe-out-chart.cpp	Sat Sep 29 12:58:04 2018 +0200
+++ b/src/relpipe-out-chart.cpp	Sat Sep 29 13:26:33 2018 +0200
@@ -24,6 +24,7 @@
 
 	WorkerThread(std::shared_ptr<RelationalReader> reader) :
 	reader(reader) {
+		setTerminationEnabled(true);
 	}
 
 	void run() {
@@ -31,6 +32,7 @@
 			reader->process();
 		} catch (RelpipeReaderException& e) {
 			// TODO: handle exception, show error dialog
+			std::wcerr << L"RelpipeReaderException: " << e.getMessge() << std::endl;
 		}
 	}
 };
@@ -67,5 +69,13 @@
 	app.exec();
 	//return app.exec();
 
+	if (t.isRunning()) {
+		std::wcerr << L"Background RelationalReader thread is still running → terminate()" << std::endl;
+		t.terminate();
+		std::wcerr << L"Background RelationalReader thread was terminated → wait()" << std::endl;
+		t.wait();
+		std::wcerr << L"Background RelationalReader thread wait() finished" << std::endl;
+	}
+
 	return resultCode;
 }