Problem with exiting GUI and canvas toolbar

Hello! I have created a GUI that displays a histogram, with an embedded TCanvas. If I toggle the toolbar (either in the code or while the GUI is open), then I get an " *** Break *** segmentation violation" error message when using the exit button. If I don’t toggle the toolbar at all, it closes without error. Curiously, this doesn’t happen for the Event Status Bar, which I can toggle or have showing when I exit, no problem. I am not sure what is causing this error. Any insight would be appreciated!

#include "viewhist.h"
TH1D *gE = new TH1D("gE","gE",4096,0,4096);

viewhist::viewhist(const TGWindow *p, UInt_t w, UInt_t h)
{
  fMain = new TGMainFrame(p,w,h);
  //main canvas
  TGHorizontalFrame *hframe0 = new TGHorizontalFrame(fMain,200,40); //fMain is the parent, 200 is width in pixels, 40 is height in pixels 
  //make it an embedded canvas - necessary to get toolbar 
  hframe0->SetEditable();
  TCanvas *fEcanvas=new TCanvas("ECanvas","ECanvas",800,600);
  hframe0->SetEditable(0);
  fMain->AddFrame(hframe0, new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 10, 10, 10,1));//10 pixels of padding on left, right, and top. 1 pixel padding on the bottom

  //exit button
  TGHorizontalFrame *hframe1 = new TGHorizontalFrame(fMain,200,40);
  TGTextButton *exit = new TGTextButton(hframe1, "&Exit");
  exit -> Connect("Clicked()", "viewhist", this, "deletebutton()");
  hframe1->AddFrame(exit, new TGLayoutHints(kLHintsCenterX, 5,5,3,4));
  fMain->AddFrame(hframe1, new TGLayoutHints(kLHintsCenterX,2,2,2,2));

  //get the histogram from the file
  TFile *_file0 = TFile::Open("hgE.root");
  if (_file0)
  {
    gE = (TH1D*)_file0->Get("gE");
    if (!gE){ //to make sure file and hist exist
      std::cout<<"no histogram found"<<std::endl;
    }
  }
    else
      std::cout<<"no file found"<<std::endl;
    gE->SetDirectory(0);//detaches the histogram from the file, so we can close the file but still have gE
    _file0->TFile::Close();

  cgE=fEcanvas->GetCanvas();
  cgE->SetTitle("Canvas gE");
  cgE->ToggleEventStatus(); //Event Status Bar
  cgE->ToggleToolBar(); //Tool Bar
  cgE->Draw();
  gE->Draw();

  fMain->SetCleanup(kDeepCleanup);
  //name the main frame
  fMain->SetWindowName("Viewing hist - gE");
  //Map all subwindows of the main frame
  fMain->MapSubwindows();
  //initialize the layout algorithm
  fMain->Resize(fMain->GetDefaultSize());
  //map main frame
  fMain->MapWindow();
}

void viewhist::deletebutton()
{
  gApplication->Terminate();
}

viewhist::~viewhist()
{
  fMain->Cleanup();
  //delete fMain;
  free(fMain);
}
int main(int argc, char *argv[])
{
  TApplication theApp("App",&argc,argv);
  std::cout <<"Launching... " <<std::endl;
  new viewhist(gClient->GetRoot(),200,200);
  theApp.Run();

  return 0;
}

Thank you!

ROOT Version: 6.14/06
Platform: CentOS Linux 7 (Core)
Compiler: g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)

Welcome to the ROOT Forum!
First of all, can you remove fMain->SetCleanup(kDeepCleanup); and try again?

That worked, thank you very much!

1 Like

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