# Problem in loading a Macro more than on time

**URL:** <https://root-forum.cern.ch/t/problem-in-loading-a-macro-more-than-on-time/56305>\
**Category:** Newbie\
**Created:** [September 7, 2023, 7:05pm UTC](https://root-forum.cern.ch/t/problem-in-loading-a-macro-more-than-on-time/56305 "2023-09-07T19:05:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GIULIO\_89](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/giulio_89/32/34291_2.png) [@GIULIO\_89](https://root-forum.cern.ch/u/GIULIO_89)\
**Post date:** [September 7, 2023, 7:05pm UTC](https://root-forum.cern.ch/t/problem-in-loading-a-macro-more-than-on-time/56305/1 "2023-09-07T19:05:31Z")

</div>

\_ROOT Version: 6.20/02  
_Platform:_ Ubuntu 20 on WSL2  
_Compiler:_ Not Provided

* * *

Hi all,

I am having some issue with a simple Macro i wrote. In principle it should be able to create an histogram, fill it randomly with a gaussian distribution, then fit it and save it to a file.  
When I run the macro the first time (using .L ) I don’t have any problems, and it works properly,  
but when I try to run it again I end up with several memory errors.  
After some try i discovered that the problem occurs when I set both Fit and Write.

Here is the code:

````plaintext
 auto *file = new TFile("file.root","RECREATE");   
  //std::unique_ptr<TFile> file(TFile::Open("file.root","RECREATE"));
  auto *h1= new TH1D("h1","Random Gaus",1000,-10,10);
  auto *c1=new TCanvas();
  TRandom3 rnd;
  double value=0;
  for(int i=0;i<1000;i++){
  value=rnd.Gaus(0,1);  
  h1->Fill(value);
  }
   auto *g= new TF1("g","gaus",-1,1);
   g->SetParameter(1,1);

  h1->Fit("g"); //when I put both Write and Fit have the memory problem
  h1->Write(); 
  file->Close();

 //delete h1;
 delete g;
 delete file;
 delete c1;

return 0;
} ``` 

````

The error message is quite long but it begins with

\*\*\* Break \*\*\* segmentation violation  
Generating stack trace…  
and it consists in line such as  
0x0000563efa1d9baf in from.

Thanks to anybody will read this.

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [September 8, 2023, 8:36am UTC](https://root-forum.cern.ch/t/problem-in-loading-a-macro-more-than-on-time/56305/2 "2023-09-08T08:36:42Z")

</div>

Hi @GIULIO_89,

welcome to the root forum. I tried running your code and it works well for me, I don’t get any memory errors (I loaded it and tried running a few times). but maybe simplifying your code a bit could help, maybe try something like:

```auto
void TestFit_Write()

{    
    TH1F h("my_histogram","histogram",1000,-10,10);

    // Let's fill it randomly
    h.FillRandom("gaus");

    auto *g= new TF1("g","gaus",-1,1);
    g->SetParameter(1,1);

    h.Fit("g");

    // Let's open a TFile
    TFile file("file.root","RECREATE");

    // Write the histogram in the file
    h.Write();

    // Close the file
    file.Close();

}

```

Cheers,  
Marta
