Strange behavior with TChain and friends

I’m having a very odd problem. I have the following code:

      fSubEvents = new TChain("CMSJetSubTree", "sub tree");
      fSubEvents->Add(Form("%s/%s", fDir->Data(), fEvent->GetSubFile()));
      printf("Added %s/%s\n", fDir->Data(), fEvent->GetSubFile());

      fSubEvents->Print();

      fSubEvents->GetEntry(fSubEvents->LoadTree(0));
      fSubEvents->SetBranchAddress("CMSJetEventInfo", &fEventInfo);
      fSubEvents->SetBranchAddress("CMSJet", &fSubJet);
      fSubEvents->SetBranchAddress("CMSJetInfo", &fSubJetInfo);
      fSubEvents->GetEntry(fSubEvents->LoadTree(0));                    // Crashes here

When originally tested, this code worked, but adding a friend to an unrelated tree (which stores the fEvent branch) causes it to crash.
I’ve verified that the same file is loaded in both cases and that the requested branches are in the chain.
Also interesting is that it crashes on the second GetEntry() rather than the first.
The tree that is having a friend added is stored in the same file as the tree loaded into
fSubEvents, but I can’t think of a reason that would matter.
Is there something I’m missing? What would make GetEntry() crash?

[quote]The tree that is having a friend added is stored in the same file as the tree loaded into fSubEvents, but I can’t think of a reason that would matter. [/quote]It could, if we have a problem in the logic for opening and closing the associated files (which would imply the deletion of the associate trees).

fSubEvents->GetEntry(fSubEvents->LoadTree(0));This code does what you (might) expect only if the parameter of LoadTree (in this case 0) points to the first tree of the chain. This is because the return value of LoadTree is the entry number within the current tree (See documentation for more details).

What would make GetEntry() crash?
One possibility is that fEventInfo, fSubJet or fSubJetInfo are not initialized properly. The best is to set them to zero. Alternatively they should point to properly initialized object.
Also (see above remarks) their might be a problem in the file opening/closing logic. If this is the case I would need for your to prepare a small example to reproduce the problem.

Cheers,
Philippe.

PS. Note that you did not mention which version of ROOT you are testing with.