SetDirectory(file), segfault at writing tree

Hi,

I’m having trouble trying to come up with a script short enough for posting, but let my try to just describe the problem.

I have a class describing a tree which is initialized in the main program. In a subroutine, I open a file (declared global) for writing and switch to it:

  outFile = new TFile(outFileName,"recreate");	//open output file 
  fTree->SetDirectory(outFile);

Then, at the end when I do:

  outFile->cd();
  fTree->Write();
  outFile->Write();
  outFile->Close();

it segfaults at the writing stage. This does not happen if I exclude the setdirectory statement from the upper piece of code (then I have other problems).
I tried putting a strange statement

outFile = fTree->GetCurrentFile();

after SetDirectory, which did not help. Do you have any suggestions? I’ll try to see whether the code can be reduced to “postable” size, but I do not have much hope.

Slava

Let me correct myself. The segfault happens not at writing, but when the program exits

Solved. Surprisingly, in the usual:

  if(!fTree) return;
  delete fTree;

in the class destructor, fTree was not NULL although the file to which I was writing it had been closed, and I expected it to delete the tree. Consequently, this caused the aforementioned segfault.

Hi,

just because the object a pointer points to is deleted doesn’t mean tht the pointer becomes 0. Example:

double* a = new double[12];
double* b = a;
delete [] b;
printf("1.: a=%p, b=%p\n", a, b);
b = 0;
printf("2.: a=%p, b=%p\n", a, b);

In 1., none of the are 0, in 2. only a is 0.

Cheers, Axel.