Deleting TCanvas

Hi,
I have a problem in deleting a TCanvas. I’m writing a code which loops over some events and for each event I would like to open a canvas, analyse the event, reset the content and move to the following event.
But whenever I try to delete my canvas, I get the following error message when my code reaches the end:

root [3] Error in : BadWindow (invalid Window parameter) (TGDockHideButton XID: 44040351, XREQ: 2)

Here is a small piece of the code which gives me this error, I might be doing something very silly:

#include “TH2F.h”
#include “TCanvas.h”
#include “TROOT.h”
#include “TH1.h”
#include “Riostream.h”
#include
#include
#include
#include
#include <string.h>
#include

Int_t AZ1[4] = {3,1,2,3};
Int_t AZ2[3] = {2,3,4};

void test()
{
TString HistoName;
Int_t *momA;
TCanvas *canv ;

TH2F **ChIoSi;

for(int k=1;k<3;k++)//loop on Z : each Z is a new event
{
int z=k;
cout<<“"<<endl;
cout<<" Z = "<<z<<endl;
cout<<"
”<<endl<<flush;
momA = NULL;

if(z==1) momA = AZ1;
else if(z==2) momA =AZ2;

ChIoSi = new TH2F*[momA[0]];

// canv= new TCanvas(Form(“canv%d”,z),Form(“canv%d”,z));
canv= new TCanvas(“canv”,“canv”);

int row = 2*momA[0];
canv->Divide(row,3);

for(int j=0;j<momA[0];j++)//loop on A
{
ChIoSi[j]=NULL;

 //Reading some histos from a file.....
  ChIoSi[j] = new TH2F("testh","testh",10,0,10,10,0,10);
  ChIoSi[j] ->SetBinContent(4,6,10);
 //--------------------------
  	     
  canv->cd(j+1);
  if(ChIoSi[j]!=NULL)
{
  ChIoSi[j]->Draw("zcol");
}
  else cout<<"chiosi = null"<<endl;

   canv->Modified();
   canv->Update();
 
 }//loop on A

//—need to reset everything before going to the next even
ChIoSi = NULL;

if(canv!=NULL)
{
//canv->Clear();
canv->Closed();
//canv = NULL;
delete canv;
}
//--------------------------------
} //loop on Z

cout<<“OUT”<<endl<<flush;

}

I always reach the end of the code (ie I see the output “OUT” before the error message shows up.

Thank you very much for your help
Best regards

Paola

When I execute you macro on my Mac with the latest ROOT version i do not get any crash. The output is:

Dear Couet,
Thank you for your quick anwer!
The root version I use is 5.32/01.
I run the code again this morning but I still get the same crash… Any idea?
Thank you
Best regards
Paola

PS I run on Fedora 15 (if it matters)

root [2] test()


           Z = 1

Warning in TROOT::Append: Replacing existing TH1: testh (Potential memory leak).
Warning in TROOT::Append: Replacing existing TH1: testh (Potential memory leak).


           Z = 2

Warning in TROOT::Append: Replacing existing TH1: testh (Potential memory leak).
Warning in TROOT::Append: Replacing existing TH1: testh (Potential memory leak).
OUT
root [3] Error in : BadWindow (invalid Window parameter) (TGDockHideButton XID: 44040351, XREQ: 2)
TGDockHideButton: 44040351
TGCompositeFrame: 44040350
Error: Function �() is not defined in current scope :0:
Warning: Automatic variable S� is allocated :0:
Error: Undeclared variable S� :0:
signal(11) Error: Segmentation violation :0:

Press return or process will be terminated in 10 sec by timeout.

cint>

Signal(14) Error time out. Exit program.

I have cleaned up a bit your macro:

{
   TString HistoName;
   Int_t *momA;
   TCanvas *canv;
   
   Int_t AZ1[4] = {3,1,2,3};
   Int_t AZ2[3] = {2,3,4};

   TH2F **ChIoSi;

   for (int k=1; k<3; k++) {
      int z=k;
      cout<<"______________________________________________"<<endl;
      cout<<" Z = "<<z<<endl;
      cout<<"______________________________________________"<<endl<<flush;
      momA = NULL;

      if      (z==1) momA = AZ1;
      else if (z==2) momA = AZ2;

      ChIoSi = new TH2F*[momA[0]];

      canv = new TCanvas("canv","canv");

      int row = 2*momA[0];
      canv->Divide(row,3);

      for (int j=0;j<momA[0];j++) {
         ChIoSi[j] = NULL;
         ChIoSi[j] = new TH2F(Form("testh%d",10*k+j),"testh",10,0,10,10,0,10);
         ChIoSi[j]->SetBinContent(4,6,10);

         canv->cd(j+1);
         if (ChIoSi[j]) ChIoSi[j]->Draw("zcol");
         else cout<<"chiosi = null"<<endl;

         canv->Modified();
         canv->Update();
      }

      ChIoSi = NULL;

      if (canv) canv->Close();
   } 
   cout<<"OUT"<<endl<<flush;
}

is it better ?
It gives me:

______________________________________________
 Z = 1
______________________________________________
______________________________________________
 Z = 2
______________________________________________
OUT
root [1] 

Hi,
thank you, but it still gives me:

Processing test.cpp...
______________________________________________
 Z = 1
______________________________________________
______________________________________________
 Z = 2
______________________________________________
OUT
root [2] Error in <RootX11ErrorHandler>: BadWindow (invalid Window parameter) (TGDockHideButton XID: 44040351, XREQ: 2)
TGDockHideButton:       44040351
        TGCompositeFrame:       44040350
Error: Function �() is not defined in current scope  :0:
Warning: Automatic variable S� is allocated :0:
Error: Undeclared variable S� :0:
signal(11) Error: Segmentation violation :0:


Press return or process will be terminated in 10 sec by timeout.

cint> 

Signal(14) Error time out. Exit program.

and it kicks me out of root… I copied your version, copy-paste…

I noticed that if I change the name of the canvas:

canv = new TCanvas(Form("canv%d",z),Form("canv%d",z))

the code doesn’t crash. But I cannot close/delete the canvases with:

    if(canv!=NULL) 
      { 
	canv->Clear();
	canv->Closed();
	canv = NULL;
      }

and I end up with 2 (but in the case of the real code they are many more than 2) canvases at the end.

Thank you very much
Paola

I guess we will have to try on fedora

Ok, thank you!!
I thought I was doing something silly but… If I find the solution I’ll let you know!
Thanks
Best regards
Paola

Hi,

I will investigate what’s going on, but could you check in your $ROOTSYS/etc/system.rootrc, or in your custom ~/.rootrc if you have one, that the Canvas.ShowToolBar option is set to false:Canvas.ShowToolBar: falseAnd try again?

Cheers, Bertrand.

Hi Bertrand,
thank you very much for your help.

Yes, I do have in the $ROOTSYS/etc/system.rootrc the Canvas.ShowToolBar option set to false.
If it can help, these are all the options Canvas related that I have there:

Canvas.MoveOpaque:          false
Canvas.ResizeOpaque:        false
Canvas.UseScreenFactor:     false
Canvas.HighLightColor:      2
Canvas.ShowEventStatus:     false
Canvas.ShowToolTips:        false
Canvas.ShowToolBar:         false
Canvas.ShowEditor:          false
Canvas.AutoExec:            true
Canvas.PrintDirectory:      .
Canvas.SavePrecision:       7
Canvas.Style:               Modern

Rint.Canvas.HighLightColor:      5

Thank you very much
Cheers
Paola

Hi Paola,

That’s weird, since I can only reproduce the problem with the toolbar being shown. Are you sure to nit have a custom $HOME/.rootrc? When you create a canvas (simply new TCanvas) in ROOT, does it have a toolbar?

Cheers, Bertrand.

Hi Paola,

Did you build ROOT from source? If yes, could you try to apply a patch?

Cheers, Bertrand.

Hi Bertrand,
you are right, the tool bar shows up when opening a new Canvas and the option was “hidden” in the rootlogon.C
Now it works!
Thank you very much!!!
Cheers and have a nice day

Paola

Hi Bertrand,
no, I didn’t built it from source. Actually I didn’t do the installation at all, the system administrator did it.
If it may help I can ask him to apply the patch (tomorrow, today is holidays in France and the lab is empty) and see if the problem persists. Please, let me know if you want us to try with the patch.
Thank you again
Cheers
Paola

Hi Paola,

OK, no problem don’t worry, I’ll make tests and submit the patch (now I know it is really due to the toolbar :wink:)
Thanks anyway for the proposal :slight_smile:
And you’re very welcome!

Cheers, Bertrand.

Thank you again Bertrand!
Ciao
Paola

You’re very welcome! :slight_smile: And FYI, a patch has just been submitted in svn trunk (rev. 44173)

Cheers, Bertrand.