# Segmentation violation with graph

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979>\
**Category:** Newbie\
**Created:** [January 6, 2021, 4:39pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979 "2021-01-06T16:39:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Repucci](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jacob_repucci/32/24488_2.png) [@Jacob\_Repucci](https://root-forum.cern.ch/u/Jacob_Repucci)\
**Post date:** [January 6, 2021, 4:39pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/1 "2021-01-06T16:39:53Z")

</div>

* * *

_ROOT Version:_ 6.22  
_Platform:_ Linux  
_Compiler:_ Not Provided

* * *

Hey all,

I’m currently running a program that has two histograms, one of which I draw a fit graph on. The first time I run it, it works fine, but if I try to load the macro again I get a segmentation violation error. When I quit root, I further get an error saying “double free or corruption (!prev)”. This only happens when I add the fit graph; without it, the code runs fine. Here’s the code in charge of graphing:

```auto
//create canvas
	auto c = new TCanvas("Z Boson Data", "Z Boson Data", 1200, 600);
	c->Divide(1, 2);

	//graph mass histogram and fit Breit-Wigner to it
	c->cd(1);
	TF1 *func = new TF1("mybw",mybw,mass_bound_low, mass_bound_high, 3);
	func->SetParameter(1, resonance_width); //get gamma/resonance width for parameter 1
	func->SetParameter(2, mass_hist->GetMean()); //get M/Z mass for parameter 2
	auto mass_fit = mass_hist->Fit("mybw", "QR");
	//mass_fit->DrawClone("Same");
	mass_hist->DrawClone("Same");

	//graph momentum histogram
	c->cd(2);
	p_hist->DrawClone("Same");

```

I’m not double-freeing any of these graphs with a delete statement, so I don’t know why I would be getting such an error.

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 6, 2021, 7:06pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/2 "2021-01-06T19:06:10Z")

</div>

Why DrawClone and not simply Draw ?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 7, 2021, 3:37pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/3 "2021-01-07T15:37:04Z")

</div>

I tried with this code:

```auto
// t.cxx
double mybw(double *px, double p[3]) {
  double x = *px;
  return x + p[0] + x*p[1] + x*x*p[2];
}

void t() {
  auto mass_hist = new TH1F("mass_hist", "mass_hist", 10, 0., 1.);
  mass_hist->Fill(0.21, 20);
  auto c = new TCanvas("Z Boson Data", "Z Boson Data", 1200, 600);
  c->Divide(1, 2);

  //graph mass histogram and fit Breit-Wigner to it
  c->cd(1);
  TF1 *func = new TF1("mybw",mybw,0.1, 1., 3);
  auto mass_fit = mass_hist->Fit("mybw", "QR");
  mass_hist->DrawClone("Same");

  //graph momentum histogram
  c->cd(2);
  auto p_hist = new TH1F("p_hist", "p_hist", 10, 0., 1.);
  p_hist->DrawClone("Same");
}

```

```auto
$ root
root [0] .x t.cxx
root [1] .x t.cxx
root.exe: /home/axel/build/root/master/src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1519: void cling::Interpreter::unload(cling::Transaction&): Assertion `!T.getTopmostParent()->getNext() && "Can not revert previous transactions"' failed.

```

So there’s indeed a problem. Is this comparable to what you run in reality?

---

<div class="post-metadata">

**Author:** ![Jacob\_Repucci](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jacob_repucci/32/24488_2.png) [@Jacob\_Repucci](https://root-forum.cern.ch/u/Jacob_Repucci)\
**Post date:** [January 7, 2021, 4:30pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/4 "2021-01-07T16:30:11Z")

</div>

The code is comparable, but the error message is not. My error message tells me that there was a segmentation violation, that root crashed, and gives me a “stack trace of all threads.”

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 8, 2021, 8:13am UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/5 "2021-01-08T08:13:09Z")

</div>

We will need code that reproduces this issue. Could you share that with us?

---

<div class="post-metadata">

**Author:** ![Jacob\_Repucci](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jacob_repucci/32/24488_2.png) [@Jacob\_Repucci](https://root-forum.cern.ch/u/Jacob_Repucci)\
**Post date:** [January 8, 2021, 7:39pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/6 "2021-01-08T19:39:35Z")

</div>

I’m using DrawClone because I’ve had problems with the graph where if I don’t use DrawClone, then the p\_hist histogram simply won’t display. This is also something that wasn’t a problem before I used the fit graph.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 9, 2021, 10:10am UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/7 "2021-01-09T10:10:20Z")

</div>

As @Axel requested, can you post the code reproducing your problem ?

---

<div class="post-metadata">

**Author:** ![Jacob\_Repucci](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jacob_repucci/32/24488_2.png) [@Jacob\_Repucci](https://root-forum.cern.ch/u/Jacob_Repucci)\
**Post date:** [January 13, 2021, 3:49pm UTC](https://root-forum.cern.ch/t/segmentation-violation-with-graph/42979/8 "2021-01-13T15:49:08Z")

</div>

Sorry for taking so long to respond, here’s my code!  
[zboson\_pt.C](https://root-forum.cern.ch/uploads/short-url/tNKsZAkDdQ89QqF8kQHieHrduXB.C) (6.3 KB)
