Avoid crashing program if crash in macro?

Dear all,

I have a program that loads the macros written by users and execute them. If a macro has a faulty code that crashes (e.g. null pointer use) the whole program crashes when call ProcessLine.

Is there a way to avoid that and run the macro in “isolation” ?

Thank you
Barth

Dear Barth,

you could run the macros with their own instance of the root interpreter, e.g., by
calling root via gSystem->Exec(“root -l -b -x -q macro.C”)

Cheers,
Jochen

EDIT:

  • updated parameter -x to tell root to exit on exception
  • added a proof of concept macro. If you execute it with gSystem->Exec() it crashes, but the interpreter stays “intact” and returns to the root prompt.
  • updated parameter -q to tell root to exit after macro :slight_smile:
    crash.C (48 Bytes)

Hi,

Thank you very much for your quick reply.

The problem I have is that I have to pass parameters to the macro and these parameters are pointers. I don’t think it is possible in the case they have their own instance of root interpreter.

For records the macro looks like

[code]#include
#include
#include
#include

using namespace std;

void crash(map<string, float>* lastValues)
{
cout << “HELLO FROM MACRO” << endl;
cout << "macro : last value test : " << (*lastValues)[“test”] << endl;
int *i = 0 ;
(*i) = 1000;
}
[/code]

And the code to call it is :

  map<string, float> lastValues;
  lastValues["test"] = 10.;
  gROOT->LoadMacro("crash.C"); // ok 
  char* s = Form("crash((map<string, float>*)%p)", &lastValues);
  gROOT->ProcessLine(s); // crash

Cheers,
Barth

Hi,

yes, this is a problem. The only “hack” I can come up with would be to build another wrapper:

On the top level you have your application with the test data. The test data gets written to a temporary file. The wrapper loads the data from the file and executes the macro as you did before. If you call the wrapper via gSystem->Exec(), the whole wrapper/macro thing will die, but your main application keeps running.

Cheers,
Jochen

EDIT: You might want to have a look at TMapFile to exchange data between two processes. Although it appears TMapFile is specific to TObjects…