From 5acd195d0fe7dffc36daaada39d0d34ae87b8e1b Mon Sep 17 00:00:00 2001
From: pwpiwi <pwpiwi@users.noreply.github.com>
Date: Thu, 1 Jun 2017 15:35:19 +0200
Subject: [PATCH] fixing the crash on exit

---
 client/Makefile      |  2 +-
 client/proxgui.cpp   | 38 +++++++++++++++++++++--------
 client/proxgui.h     |  2 +-
 client/proxguiqt.cpp |  3 ++-
 client/proxguiqt.h   | 13 ++++++++++
 client/proxmark3.c   | 57 ++++++++++++++++----------------------------
 client/proxmark3.h   |  9 +++++++
 7 files changed, 75 insertions(+), 49 deletions(-)

diff --git a/client/Makefile b/client/Makefile
index 3faa6f93..89f97e52 100644
--- a/client/Makefile
+++ b/client/Makefile
@@ -20,7 +20,7 @@ LDLIBS = -L/opt/local/lib -L/usr/local/lib -lreadline -lpthread -lm
 LUALIB = ../liblua/liblua.a
 LDFLAGS = $(COMMON_FLAGS)
 CFLAGS = -std=c99 -D_ISOC99_SOURCE -I. -I../include -I../common -I../zlib -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -g -O3
-CXXFLAGS = -Wall -O3
+CXXFLAGS = -I../include -Wall -O3
 
 LUAPLATFORM = generic
 platform = $(shell uname)
diff --git a/client/proxgui.cpp b/client/proxgui.cpp
index 7dcb0da0..e7fdae6c 100644
--- a/client/proxgui.cpp
+++ b/client/proxgui.cpp
@@ -10,8 +10,22 @@
 
 #include "proxgui.h"
 #include "proxguiqt.h"
+#include "proxmark3.h"
 
 static ProxGuiQT *gui = NULL;
+static WorkerThread *main_loop_thread = NULL;
+
+WorkerThread::WorkerThread(char *script_cmds_file, bool usb_present) : script_cmds_file(script_cmds_file), usb_present(usb_present)
+{
+}
+
+WorkerThread::~WorkerThread() 
+{
+}
+
+void WorkerThread::run() {
+	main_loop(script_cmds_file, usb_present);
+}
 
 extern "C" void ShowGraphWindow(void)
 {
@@ -39,31 +53,35 @@ extern "C" void RepaintGraphWindow(void)
 
 extern "C" void MainGraphics(void)
 {
-  if (!gui)
-    return;
+	if (!gui)
+		return;
 
-  gui->MainLoop();
+	main_loop_thread->start();
+	gui->MainLoop();
 }
 
-extern "C" void InitGraphics(int argc, char **argv)
+extern "C" void InitGraphics(int argc, char **argv, char *script_cmds_file, bool usb_present)
 {
 #ifdef Q_WS_X11
-  bool useGUI = getenv("DISPLAY") != 0;
+	bool useGUI = getenv("DISPLAY") != 0;
 #else
-  bool useGUI = true;
+	bool useGUI = true;
 #endif
-  if (!useGUI)
-    return;
+	if (!useGUI)
+		return;
 
-  gui = new ProxGuiQT(argc, argv);
+	gui = new ProxGuiQT(argc, argv);
+	main_loop_thread = new WorkerThread(script_cmds_file, usb_present);
+	QObject::connect(main_loop_thread, SIGNAL(finished()), main_loop_thread, SLOT(deleteLater()));
+	QObject::connect(main_loop_thread, SIGNAL(finished()), gui, SLOT(_Exit()));
 }
 
+
 extern "C" void ExitGraphics(void)
 {
   if (!gui)
     return;
 
   gui->Exit();
-  //delete gui;
   gui = NULL;
 }
diff --git a/client/proxgui.h b/client/proxgui.h
index 74c5063c..6e261cb9 100644
--- a/client/proxgui.h
+++ b/client/proxgui.h
@@ -19,7 +19,7 @@ void ShowGraphWindow(void);
 void HideGraphWindow(void);
 void RepaintGraphWindow(void);
 void MainGraphics(void);
-void InitGraphics(int argc, char **argv);
+void InitGraphics(int argc, char **argv, char *script_cmds_file, bool usb_present);
 void ExitGraphics(void);
 
 #define MAX_GRAPH_TRACE_LEN (40000*8)
diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp
index 3805411d..506b8138 100644
--- a/client/proxguiqt.cpp
+++ b/client/proxguiqt.cpp
@@ -84,6 +84,7 @@ void ProxGuiQT::_HideGraphWindow(void)
 void ProxGuiQT::_Exit(void) {
 	delete this;
 }
+
 void ProxGuiQT::MainLoop()
 {
 	plotapp = new QApplication(argc, argv);
@@ -110,7 +111,7 @@ ProxGuiQT::~ProxGuiQT(void)
 	//}
 	if (plotapp) {
 		plotapp->quit();
-		delete plotapp;
+		// delete plotapp;
 		plotapp = NULL;
 	}
 }
diff --git a/client/proxguiqt.h b/client/proxguiqt.h
index b3a3e85a..73f9286c 100644
--- a/client/proxguiqt.h
+++ b/client/proxguiqt.h
@@ -118,4 +118,17 @@ class ProxGuiQT : public QObject
 		void HideGraphWindowSignal(void);
 		void ExitSignal(void);
 };
+
+
+class WorkerThread : public QThread {
+	Q_OBJECT;
+public:
+	WorkerThread(char*, bool);
+	~WorkerThread();
+	void run();
+private:
+	char *script_cmds_file = NULL;
+	bool usb_present = false;
+};
+
 #endif // PROXGUI_QT
diff --git a/client/proxmark3.c b/client/proxmark3.c
index d00af5cc..460aea29 100644
--- a/client/proxmark3.c
+++ b/client/proxmark3.c
@@ -57,11 +57,6 @@ struct receiver_arg {
 	int run;
 };
 
-struct main_loop_arg {
-	int usb_present;
-	char *script_cmds_file;
-};
-
 byte_t rx[0x1000000];
 byte_t* prx = rx;
 
@@ -97,13 +92,13 @@ static void *uart_receiver(void *targ) {
 	return NULL;
 }
 
-static void *main_loop(void *targ) {
-	struct main_loop_arg *arg = (struct main_loop_arg*)targ;
+
+void main_loop(char *script_cmds_file, bool usb_present) {
 	struct receiver_arg rarg;
 	char *cmd = NULL;
 	pthread_t reader_thread;
 
-	if (arg->usb_present == 1) {
+	if (usb_present) {
 		rarg.run = 1;
 		pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
 		// cache Version information now:
@@ -113,10 +108,10 @@ static void *main_loop(void *targ) {
 	FILE *script_file = NULL;
 	char script_cmd_buf[256];  // iceman, needs lua script the same file_path_buffer as the rest
 
-	if (arg->script_cmds_file) {
-		script_file = fopen(arg->script_cmds_file, "r");
+	if (script_cmds_file) {
+		script_file = fopen(script_cmds_file, "r");
 		if (script_file) {
-			printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
+			printf("using 'scripting' commands file %s\n", script_cmds_file);
 		}
 	}
 
@@ -171,19 +166,16 @@ static void *main_loop(void *targ) {
   
 	write_history(".history");
   
-	if (arg->usb_present == 1) {
+	if (usb_present) {
 		rarg.run = 0;
 		pthread_join(reader_thread, NULL);
 	}
 	
-	ExitGraphics();
-	
 	if (script_file) {
 		fclose(script_file);
 		script_file = NULL;
 	}
-	pthread_exit(NULL);
-	return NULL;
+
 }
 
 static void dumpAllHelp(int markdown)
@@ -224,6 +216,7 @@ static void set_my_executable_path(void)
 	}
 }
 
+
 int main(int argc, char* argv[]) {
 	srand(time(0));
   
@@ -249,25 +242,20 @@ int main(int argc, char* argv[]) {
 
 	set_my_executable_path();
 	
-	// Make sure to initialize
-	struct main_loop_arg marg = {
-		.usb_present = 0,
-		.script_cmds_file = NULL
-	};
-	pthread_t main_loop_thread;
-
+	bool usb_present = false;
+	char *script_cmds_file = NULL;
   
 	sp = uart_open(argv[1]);
 	if (sp == INVALID_SERIAL_PORT) {
 		printf("ERROR: invalid serial port\n");
-		marg.usb_present = 0;
+		usb_present = false;
 		offline = 1;
 	} else if (sp == CLAIMED_SERIAL_PORT) {
 		printf("ERROR: serial port is claimed by another process\n");
-		marg.usb_present = 0;
+		usb_present = false;
 		offline = 1;
 	} else {
-		marg.usb_present = 1;
+		usb_present = true;
 		offline = 0;
 	}
 
@@ -283,24 +271,21 @@ int main(int argc, char* argv[]) {
 			flushAfterWrite = 1;
 		}
 		else
-		marg.script_cmds_file = argv[2];
+		script_cmds_file = argv[2];
 	}
 
 	// create a mutex to avoid interlacing print commands from our different threads
 	pthread_mutex_init(&print_lock, NULL);
-	pthread_create(&main_loop_thread, NULL, &main_loop, &marg);
 
-	// build ui/graph forms on separate thread (killed on main_loop_thread);
-	InitGraphics(argc, argv);
+#ifdef HAVE_GUI
+	InitGraphics(argc, argv, script_cmds_file, usb_present);
 	MainGraphics();
-	//this won't return until ExitGraphics() is called
-
-	//wait for thread to finish
-	pthread_join(main_loop_thread, NULL);
-	
+#else
+	main_loop(script_cmds_file, usb_present);
+#endif	
 
 	// Clean up the port
-	if (offline == 0) {
+	if (usb_present) {
 		uart_close(sp);
 	}
 
diff --git a/client/proxmark3.h b/client/proxmark3.h
index 3f7c3004..7ff7f676 100644
--- a/client/proxmark3.h
+++ b/client/proxmark3.h
@@ -16,8 +16,17 @@
 
 #define PROXPROMPT "proxmark3> "
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 void SendCommand(UsbCommand *c);
 const char *get_my_executable_path(void);
 const char *get_my_executable_directory(void);
+void main_loop(char *script_cmds_file, bool usb_present);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif
-- 
2.39.5