Delete tree crashing

Hello,
I have to open and close many TFiles and TTrees, and I’m trying to do so without memory leaks.
What is the correct way to handle the closing of a file where a tree is open? The following code crashes with a segmentation violation:

[code]#include

#include “TApplication.h”
#include “TTree.h”
#include “TFile.h”

int main(int argc, char **argv) {
int value = 1;

TApplication app("app", &argc, argv);
TFile* root_file = new TFile("try_tree.root", "recreate");
TTree* tree = new TTree("tree", "tree");
tree->Branch("value", &value, "value/I");
tree->Fill();
tree->Fill();
tree->Fill();
root_file->Write();
root_file->Close();
std::cout << "deleting 1..." << std::endl;
delete root_file;
delete tree;

return 0;
}
[/code]

Compiled with

Remove the line “delete tree;”. Your “tree” will be automatically deleted when you “delete root_file;”.