# Segmentation violation on closing TFile

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-on-closing-tfile/42693>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [December 10, 2020, 11:20pm UTC](https://root-forum.cern.ch/t/segmentation-violation-on-closing-tfile/42693 "2020-12-10T23:20:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrTuna](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7feea3/32.png) [@mrTuna](https://root-forum.cern.ch/u/mrTuna)\
**Post date:** [December 10, 2020, 11:20pm UTC](https://root-forum.cern.ch/t/segmentation-violation-on-closing-tfile/42693/1 "2020-12-10T23:20:32Z")

</div>

Hello,  
I am having trouble with a very simple matter. The following code attempts to create a directory and save a histogram in it AND not use `new` to create the histogram. Unfortunately it gives a segfault. Why is that?

```auto
int main ()
{
  TFile *f = new TFile("test.root","RECREATE");
  f->mkdir( "0" )->cd();
  TH1F b = TH1F("bb", "bb", 1,0,1);
  f->Close();
  return 0;
}

```

Surely with

```auto
TH1F *b = new TH1F("bb", "bb", 1,0,1);

```

it works, but why? Can I still somehow fix the original example without using `new`?

I am using root 6.06 and g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [December 11, 2020, 7:59am UTC](https://root-forum.cern.ch/t/segmentation-violation-on-closing-tfile/42693/2 "2020-12-11T07:59:15Z")

</div>

Hi @mrTuna, welcome on the forum!

You facing histogram ownership problem.  
Just look in [TH1 documentation](https://root.cern/doc/master/classTH1.html#creating-histograms).  
Before creating histogram you can call:

```auto
TH1::AddDirectory(kFALSE);

```

Then your example will work.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![mrTuna](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7feea3/32.png) [@mrTuna](https://root-forum.cern.ch/u/mrTuna)\
**Post date:** [December 11, 2020, 11:18am UTC](https://root-forum.cern.ch/t/segmentation-violation-on-closing-tfile/42693/3 "2020-12-11T11:18:48Z")

</div>

Hi @linev,  
thank you for the answer. That was helpful.

For future reference I found this useful also: [ObjectOwnership](https://root.cern.ch/root/htmldoc/guides/users-guide/ObjectOwnership.html)
