# \<TROOT::Append\>: Replacing existing TH1: h (Potential memory leak)

**URL:** <https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [November 21, 2020, 3:41pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387 "2020-11-21T15:41:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [November 21, 2020, 3:41pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/1 "2020-11-21T15:41:45Z")

</div>

Hi everyone.  
I have an warning when I run my code. This warning was discussed more times in the past. I have read some posts related this topic but it can not help me to pass this problem.  
My code has only a histogram. I do not create a different histogram with a same name but the warning still appear. How can I deal with the problem?

```auto
#include <TF1.h>
#include <TH1.h>
using namespace std;

TH1D *h = new TH1D("h", "his", 10, 0, 10);

double myfunc(double x);

void main()
{
    for (int i = 1; i <= h->GetNbinsX(); i++)
    {
        h->SetBinContent(i, i);
    }
    TF1 *f = new TF1("f", "myfunc(x)", 0, 10);
    f->Draw();
}
double myfunc(double x)
{
    double q = 0;
    for (int i = 0; i < 10; i++)
    {
        double g = h->GetBinContent(i+1);
        q = q + g*x;
    }
    return q;  
}

```

> Warning in TROOT::Append: Replacing existing TH1: h (Potential memory leak).

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [November 21, 2020, 4:53pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/2 "2020-11-21T16:53:19Z")

</div>

Hi again,

I can’t reproduce your warning in ROOT 6.14/04. This is what I get, and there are no warnings/errors of any kind:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/f/8f1bb11edef549b02fd8985831201a18c8217d81.png)

Could you maybe explain how you run your code? And what is your ROOT version/platform?

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [November 21, 2020, 11:42pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/3 "2020-11-21T23:42:05Z")

</div>

Hi.  
This warning does not appear in the first time you run the code. But it appear in the next time you run the code (your IDE still opened). I am using Root 5.34.36 on Win 10, I also tried running on Ubuntu with root 6.18 anh there is a same warning.  
Could you please try again?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [November 22, 2020, 10:51am UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/4 "2020-11-22T10:51:41Z")

</div>

Okay, I don’t use any IDEs (only CLI), so I guess I need to know how to reproduce it via the command-line interface. But maybe someone else has other ideas…

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [November 22, 2020, 11:49am UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/5 "2020-11-22T11:49:28Z")

</div>

When I run code from macro ROOT, this problem also appeared in the second time. Here is photo.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/8/28fc9a4da15dc7a774375aa3fc8b9d870a7b8eea.png)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [November 22, 2020, 12:40pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/6 "2020-11-22T12:40:13Z")

</div>

Ah, of course. You have  
`TH1D *h = new TH1D("h", "his", 10, 0, 10);`  
, and running your macro twice in the same ROOT session is the same as running the following macro just once:

```auto
void example()
{
        TH1D *h = new TH1D("h", "his", 10, 0, 10);
        TH1D *h1 = new TH1D("h", "his", 10, 0, 10);
}

```

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [November 22, 2020, 1:07pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/7 "2020-11-22T13:07:50Z")

</div>

Hi.  
I do not think so. As you said, I also have a object TF1 and in the second time I think it has to appear too (but it does not appear). I saw that this warning happens only with TH object.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [November 22, 2020, 1:41pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/8 "2020-11-22T13:41:47Z")

</div>

This probably has to do with differences in object ownership between `TH1` objects and others, `TF1` in your case. Let’s wait for experts to chime in.

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [November 23, 2020, 1:50pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/9 "2020-11-23T13:50:46Z")

</div>

@couet Could you please help me?

---

<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:** [November 23, 2020, 4:12pm UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/10 "2020-11-23T16:12:26Z")

</div>

@yus is right. If you don’t need ROOT’s object lifetime management, call `TH1::AddDirectory(false)` before creating the first histogram, and you’ll get “regular C++ behavior”.

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [November 29, 2020, 3:52am UTC](https://root-forum.cern.ch/t/troot-append-replacing-existing-th1-h-potential-memory-leak/42387/11 "2020-11-29T03:52:11Z")

</div>

Hi Axel.  
Thank you. My problem is dealed.
