Quit root in macro

as suggested, how to quit a root macro as the last line in the macro has been run ?


_ROOT Version:_6.24 (conda-forge)
Platform: cenntos7
Compiler: gcc9


OK, I just use the exit() from C++

If you want to exit root after processing the command line macros, try (see “root -?”): root -q ...

If you want to exit from inside of a macro, try: gApplication->Terminate(0);

I’m not sure if this is the most secure way. Maybe @pcanal and/or @Axel know its “limitations”.

It’s actually root -q :slight_smile:

but i want to end the program within the macro. is there equivalents other than exit(0) ?

No need, exit(0) should work just fine.

@Axel If you say “exit(0);” is “fine”, why do you always use “gApplication->Terminate(0);” in ROOT itself (including tutorials and tests)?

I think these days only for historical reasons, right, @pcanal ?

We should be triggering all necessary tear down also from exit(0).

It should work just fine for most case. The two are still not completely equivalent and Terminate is ‘safer’. There can be a difference in the case where at the time of the call there is still some output TFile open. When using Terminate, ROOT will flush and close those files before the shared library are unloaded while with exit, the file will be flushed and closed only after most shared library has been unloaded. The later would cause a problem if the flushing ends up trying to write into the file some user objects for which the library has already been deleted.

#include "TApplication.h"
#include <cstdlib>

void adios(int status = 0) {
#ifdef ROOT_TApplication
  if (gApplication) gApplication->Terminate(status);
#endif
  exit(status);
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.