Cannot close file

I am using root 3.10/02 and complied the following code

    fin=new TFile(filename, "read");
    if (fin->IsOpen()) {
      t=(TTree*)fin->Get("T");
      qev=new QEvent();
      qev=0;
      t->SetBranchAddress("Events", &qev);
      delete qev;
      delete t;
    }
    fin->Close();

After the compilation, I run the executable. However it crashed with error message

*** Break *** segmentation violation
Generating stack trace…
0x42015574 in __libc_start_main + 0xe4 from /lib/tls/libc.so.6
0x080490f1 in TFile::TFile[in-charge](char const*, char const*, char const*, int) + 0x31 from ./LinkEvent
Aborted

As long as I delete the line of “SetBranchAddress”, everything is fine. However, the code is useless now.

Thanks for your help

Hi,

Did you check that the value of ‘t’ is not zero?
This could happen if the file does not exist or if the TTree is not present in the file.

Cheers,
Philippe

qev=0;
delete qev;

this never works LOL

I just made some changes to see what is wrong[/code]. The code now is

    fin=new TFile(filename, "read");
    if (fin->IsOpen()) {
      t=(TTree*)fin->Get("T");
      qev=new QEvent();
      qev=0;
      t->SetBranchAddress("Events", &qev);
      t->GetEntry(1);
      cerr<<qev<<" "<<t<<" "<<fin<<'\n';
      delete qev;
      delete t;
      cerr<<qev<<" "<<t<<" "<<fin<<'\n';
    }
    delete fin;

The output is

It looks that “delete” cannot do its job. Why?

Hi,

The stack seems odd. Could you run within the debugger and send its stack trace.

I think that the problem is doing:delete qev; delete t; is the cause of the problem. Because
of your ‘qev=0’, the Tree owns the object and probably
deletes is a second case.

In addition the tree is owned by the TFile, so you do
not have to delete it (but deleting it is okay).

Use:

fin=new TFile(filename, "read"); if (fin->IsOpen()) { t=(TTree*)fin->Get("T"); qev=0; t->SetBranchAddress("Events", &qev); } delete fin;
Cheers,
Philippe

Also doing:qev=new QEvent(); qev=0; is a memory leak. Just do one of the other.

In cint, the “delete” will set qev and t to 0, but “delete fin” will crash. If I do NOT delete qev and t, “delete fin” will work fine. However, if I compile the code and run the executable, the “delete fin” crash no matter whether I delete qev and t or not.

By the way, how to run debug mode?

Thanks

Hi,

To get a stack trace just do

> gdb root.exe gdb> run root [] ... do some thing gdb> where

Cheers,
Philippe