Allow runAsMainSingleModule to be called more than once
authorRoland Schulz <roland@utk.edu>
Sun, 21 Sep 2014 22:03:17 +0000 (18:03 -0400)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sun, 5 Oct 2014 03:58:48 +0000 (05:58 +0200)
Call finalizeForCommandLine even if an exception is thrown so that it is
OK to call runAsMainSingleModule again. This allows this (and the calling
runAsMain as e.g. used in the template) be called from some framework which
doesn't reset globals.

Change-Id: I9eff770e06feef90f99ec0445bd294816f8b6a0a

src/gromacs/commandline/cmdlineinit.cpp
src/gromacs/commandline/cmdlineinit.h
src/gromacs/commandline/cmdlinemodulemanager.cpp
src/gromacs/commandline/cmdlinemodulemanager.h
src/programs/gmx.cpp
src/testutils/testoptions.cpp

index 8fe171ee058c34e3ba807a752211baea35b95afd..003ad846c901535989875ef5900e9af1d69eeafd 100644 (file)
@@ -140,6 +140,13 @@ void finalizeForCommandLine()
     g_commandLineContext.reset();
 }
 
+int processExceptionAtExitForCommandLine(const std::exception &ex)
+{
+    int rc = processExceptionAtExit(ex); //Currently this aborts for GMX_LIB_MPI
+    finalizeForCommandLine();            //thus this MPI_Finalize doesn't matter
+    return rc;
+}
+
 int runCommandLineModule(int argc, char *argv[],
                          CommandLineModuleInterface *module)
 {
index ae4089f4aea58c462297cb1a50de6e5683a944da..0f95039cbcfb45f12afa13be5fc9f80391efa498 100644 (file)
@@ -92,7 +92,19 @@ CommandLineProgramContext &initForCommandLine(int *argc, char ***argv);
  * \ingroup module_commandline
  */
 void finalizeForCommandLine();
-
+/*! \brief
+ * Handles an exception and deinitializes after initForCommandLine.
+ *
+ * \param[in] ex  Exception that is the cause for terminating the program.
+ * \returns   Return code to return from main().
+ *
+ * This method should be called as the last thing before terminating the
+ * program because of an exception. See processExceptionAtExit() for details.
+ * Additionally this method undoes the work done by initForCommandLine.
+ *
+ * Does not throw.
+ */
+int processExceptionAtExitForCommandLine(const std::exception &ex);
 /*! \brief
  * Implements a main() method that runs a single module.
  *
index 8fee8aa10e28ca0cef593bdd1b1d1a8ffc6410e5..74113cc53126c15e5eeef8b9c595c96674c67766 100644 (file)
@@ -622,7 +622,7 @@ int CommandLineModuleManager::runAsMainSingleModule(
     catch (const std::exception &ex)
     {
         printFatalErrorMessage(stderr, ex);
-        return processExceptionAtExit(ex);
+        return processExceptionAtExitForCommandLine(ex);
     }
 }
 
index 62b39488ca28461e37673d088c16cbd36b9bc236..707d251e5e08372ddb531d1ed98fc62a5217344d 100644 (file)
@@ -82,7 +82,7 @@ typedef gmx_unique_ptr<CommandLineModuleInterface>::type
        catch (const std::exception &ex)
        {
            gmx::printFatalErrorMessage(stderr, ex);
-           return gmx::processExceptionAtExit(ex);
+           return gmx::processExceptionAtExitForCommandLine(ex);
        }
    }
  * \endcode
index 3381f6bc8e7129c755bf7831b11049e1683b0146..94a668eadb5d45e1ff0990dcf9deb890d2ece3b4 100644 (file)
@@ -64,6 +64,6 @@ main(int argc, char *argv[])
     catch (const std::exception &ex)
     {
         gmx::printFatalErrorMessage(stderr, ex);
-        return gmx::processExceptionAtExit(ex);
+        return gmx::processExceptionAtExitForCommandLine(ex);
     }
 }
index a46d8875c37797a55813eb2eefff52aad683f464..77f49b9dd1bf8df1cb7e469f4152a02147a65d8b 100644 (file)
@@ -189,7 +189,7 @@ void initTestUtils(const char *dataPath, const char *tempPath, int *argc, char *
     catch (const std::exception &ex)
     {
         printFatalErrorMessage(stderr, ex);
-        std::exit(processExceptionAtExit(ex));
+        std::exit(processExceptionAtExitForCommandLine(ex));
     }
 }