TApplication causes segmentation violation

Hi all,
I have a short analysis program that runs through some data and creates various histograms and a tree, and saves it all to a file. I’d like to open up a TBrowser at the end to be able to look at some of the histograms without having to separately run root and open the file again. So I have the following:

class AppBrowser : public TBrowser{
public:
  AppBrowser() : TBrowser(){}
  ~AppBrowser() { gApplication->Terminate();}
};  

int main(){
   TFile *rootout = new TFile("Hits.root","RECREATE");
   //do some analysis
   TApplication *app = new TApplication("app",0,0);
   new AppBrowser;
   app->Run(1);

   //Now do some cleanup
   rootout->Close();
   delete rootout; 
   //delete some other stuff
   return 0;
}

Everything compiles and works great, until I close the browser window, when it generates a segmentation fault, but with no info in the stack trace. If I go to the File menu of the browser and choose Quit ROOT, it seems to behave OK. Any suggestions here? I’m running root version 5.13 on a machine with Debian 2.14.

Thanks!

Remove “gApplication->Terminate();” from your destructor

Rene

I’ll admit I don’t totally understand the code, as I stole it from this post on this forum about integrating ROOT into an OS browser

But when I remove that line, the program simply hangs after the browser window closes, with no way to exit but ctrl-C.

Thanks again,
Ben

Hi Ben,

Please keep “gApplication->Terminate();” in your destructor…
I don’t see what’s wrong with your original code…
Maybe it depends on what you do once the browser is open…
Could you post your full code, so I can try (because this simple code works for me - at least on Windows)?
I will try on Linux asap and let you know.

Bertrand.

Hi,
Sorry for not replying; I got temporarily shifted to other projects. I tried creating a small test executable to recreate the problem, but it worked without any difficulties. Unfortunately the actual program I am using is part of a large project with many dependencies, and finding exactly where the conflict lies will take a long time. However after playing around, I found a fix (or hack) which works, which is to instantiate my TApplication before doing any analysis as in

int main(){
   TApplication *app = new TApplication("app",0,0);
   TFile *rootout = new TFile("Hits.root","RECREATE");
   //do some analysis
   new AppBrowser;
   app->Run(1);
   //Now do some cleanup
   rootout->Close();
   delete rootout;
   //delete some other stuff
   return 0;
} 

I think the reason for this is that my analysis involves some fitting and saving of canvases, which generates a separate TApplication, then, when I try to instantiate mine, it gets upset because the TApplication is supposed to have only one instance.

If this is the case, is there a way to protect against that, i.e. force the TApplication to behave as a singleton? I tried explicitly checking gApplication before instantiating:

if(gApplication) 
     gApplication->Delete();

but that still didn’t fix it.

I’m satisfied with my fix; I mostly ask out of curiosity.
Cheers,
~Ben[/code]