From 0de538c7237551340d0a54d0be4bb4f4407e8e11 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Sat, 18 Jul 2020 12:17:21 +0100 Subject: [PATCH] Close orphaned jt9 sub-process before restarting --- main.cpp | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 040225352..b3de449f6 100644 --- a/main.cpp +++ b/main.cpp @@ -353,14 +353,41 @@ int main(int argc, char *argv[]) // Multiple instances: use rig_name as shared memory key mem_jt9.setKey(a.applicationName ()); - if(!mem_jt9.attach()) { - if (!mem_jt9.create(sizeof(struct dec_data))) { - splash.hide (); - MessageBox::critical_message (nullptr, a.translate ("main", "Shared memory error"), - a.translate ("main", "Unable to create shared memory segment")); - throw std::runtime_error {"Shared memory error"}; + // try and shut down any orphaned jt9 process + for (int i = 3; i; --i) // three tries to close old jt9 + { + if (mem_jt9.attach ()) // shared memory presence implies + // orphaned jt9 sub-process + { + dec_data_t * dd = reinterpret_cast (mem_jt9.data()); + mem_jt9.lock (); + dd->ipc[1] = 999; // tell jt9 to shut down + mem_jt9.unlock (); + mem_jt9.detach (); // start again + } + else + { + break; // good to go + } + QThread::sleep (1); // wait for jt9 to end + } + if (!mem_jt9.attach ()) + { + if (!mem_jt9.create (sizeof (struct dec_data))) + { + splash.hide (); + MessageBox::critical_message (nullptr, a.translate ("main", "Shared memory error"), + a.translate ("main", "Unable to create shared memory segment")); + throw std::runtime_error {"Shared memory error"}; + } + } + else + { + splash.hide (); + MessageBox::critical_message (nullptr, a.translate ("main", "Sub-process error"), + a.translate ("main", "Failed to close orphaned jt9 process")); + throw std::runtime_error {"Sub-process error"}; } - } mem_jt9.lock (); memset(mem_jt9.data(),0,sizeof(struct dec_data)); //Zero all decoding params in shared memory mem_jt9.unlock ();