Warning in <TROOT::Append>: Replacing existing TH1: xy_panel (Potential memory leak)

This code produces the warning because I ran DumpStep() twice in my program (If I only produce 1 eps file there is no warning):

Info in <TCanvas::Print>: eps file poca_median_cache/1d.lambda.0.eps has been created
Warning in <TROOT::Append>: Replacing existing TH1: xy_panel (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: xz_panel (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: yz_panel (Potential memory leak).
Info in <TCanvas::Print>: eps file poca_median_cache/2d.lambda.0.eps has been created

I have searched for this issue and find that it is related to creating multiple objects with the same name but I do not know which is the same.

I use different lambda title directory but same step and flag in DumpStep(std::vector<double> &lambda, const char *title, const char *directory, int step, int flag)

void DrawProjection2D(vector<double> &lambda,
                      const char *title,
                      const char *filename, int flag)
{
#ifndef SIM
    const double dz = (Z3 - Z2) / Nz;
    int Nz_ = (int)((Z3 - Z2_) / dz);
#endif
#ifdef SIM
    int Nz_ = Nz;
    Z2_ = Z2;
#endif
    TH3D graph3d("result", title,
                 Nx, -Lx / 2, Lx / 2,
                 Ny, -Ly / 2, Ly / 2,
                 Nz_, Z2_, Z3);
    TStyle *default_style = new TStyle("Default", "Default Style");
    default_style->SetPalette(55);

    for (int k = 0; k < Nz_; ++k)
        for (int j = 0; j < Ny; ++j)
            for (int i = 0; i < Nx; ++i)
            {
                // remove most points in 3d graph
                double value = lambda[INDEX(i, j, k)];
                graph3d.SetBinContent(i + 1, j + 1, Nz_ - k, ((value > 1e-3) ? value : 1e-20));
            }

    // graph3d.Scale(10000 / graph3d.Integral());

    TCanvas canvas("result-canvas", title, 1600, 1600);
    canvas.Divide(2, 2);
    canvas.cd(1);
    graph3d.Draw("LEGO2");

    // we wanna draw another rotatable version in a root session
    // but TFile always had a linker error
    // TFile *stigma = new TFile("sig-data.root","recreate");
    // graph3d.Write();
    // stigma->Close();

    // do not use the Projection function of root since it always produces empty results
    // creating a new histogram can easily solve the problem
    canvas.cd(2); // X-Y Graph
    TH2D *xy_panel = new TH2D("xy_panel", "xy_panel", Nx, -Lx / 2, Lx / 2, Ny, -Ly / 2, Ly / 2);
    for (int j = 0; j < Ny; ++j)
        for (int i = 0; i < Nx; ++i)
        {
            double value = 0;
            for (int k = 0; k < Nz_; ++k)
            {
                value += lambda[INDEX(j, i, k)];
            }
            xy_panel->SetBinContent(i + 1, j + 1, ((value > 1e-20) ? value : 1e-20));
        }
    if (flag)
        xy_panel->SetMaximum(8);
    xy_panel->Draw("colz");
    xy_panel->SetStats(false);
    xy_panel->GetXaxis()->SetTitle("Y/mm");
    xy_panel->GetYaxis()->SetTitle("X/mm");
    xy_panel->GetXaxis()->CenterTitle();
    xy_panel->GetYaxis()->CenterTitle();

    canvas.cd(3); // X-Z Graph
    TH2D *xz_panel = new TH2D("xz_panel", "xz_panel", Nx, -Lx / 2, Lx / 2, Nz_, Z2_, Z3);
    for (int j = 0; j < Nz_; ++j)
        for (int i = 0; i < Nx; ++i)
        {
            double value = 0;
            for (int k = 0; k < Ny; ++k)
            {
                value += lambda[INDEX(i, k, Nz_ - j - 1)];
            }
            xz_panel->SetBinContent(i + 1, j + 1, ((value > 1e-20) ? value : 1e-20));
        }
    if (flag)
        xz_panel->SetMaximum(8);
    xz_panel->Draw("colz");
    xz_panel->SetStats(false);
    xz_panel->GetXaxis()->SetTitle("X/mm");
    xz_panel->GetYaxis()->SetTitle("Z/mm");
    xz_panel->GetXaxis()->CenterTitle();
    xz_panel->GetYaxis()->CenterTitle();

    canvas.cd(4); // Y-Z Graph
    TH2D *yz_panel = new TH2D("yz_panel", "yz_panel", Ny, -Ly / 2, Ly / 2, Nz_, Z2_, Z3);
    for (int j = 0; j < Nz_; ++j)
        for (int i = 0; i < Ny; ++i)
        {
            double value = 0;
            for (int k = 0; k < Nx; ++k)
            {
                value += lambda[INDEX(k, i, Nz_ - j - 1)];
            }
            yz_panel->SetBinContent(i + 1, j + 1, ((value > 1e-20) ? value : 1e-20));
        }
    if (flag)
        yz_panel->SetMaximum(8);
    yz_panel->Draw("colz");
    yz_panel->SetStats(false);
    yz_panel->GetXaxis()->SetTitle("Y/mm");
    yz_panel->GetYaxis()->SetTitle("Z/mm");
    yz_panel->GetXaxis()->CenterTitle();
    yz_panel->GetYaxis()->CenterTitle();

    for (int i = 1; i <= 4; ++i)
    {
        auto *pad = canvas.GetPad(i);
        pad->SetLeftMargin(0.15);
        pad->SetRightMargin(0.17);
        pad->SetTopMargin(0.12);
        pad->SetBottomMargin(0.11);
    }

    // canvas.TAttPad::SetRightMargin(0.11);
    canvas.SetTopMargin(0.12);
    canvas.SetBottomMargin(0.11);

    canvas.Draw();
    canvas.SaveAs(filename);
}

void DumpStep(std::vector<double> &lambda,
              const char *title,
              const char *directory,
              int step, int flag)
{
    try
    {
        if (!fs::exists(directory))
            fs::create_directory(directory);
        fs::path dir(directory);
        string filename = "lambda." + to_string(step);
        ofstream fout((dir / filename).string());
        if (fout)
        {
            fout.precision(12);
            fout << Nx << ' ' << Ny << ' ' << Nz << '\n'
                 << Lx << ' ' << Ly << '\n'
                 << Z1 << ' ' << Z2 << ' ' << Z3 << ' ' << Z4 << '\n';
            for (auto value : lambda)
                fout << value << '\n';
        }
        fout.close();
        DrawProjection1D(lambda, title,
                         (dir / ("1d." + filename + ".eps")).string().c_str());
        DrawProjection2D(lambda, title,
                         (dir / ("2d." + filename + ".eps")).string().c_str(), flag);
    }
    catch (...)
    {
        cerr << "Can not dump for step " << step << endl;
    }
}

For these three “AB_panel” histograms, use:

delete gROOT->FindObject("AB_panel"); // prevent "memory leak"
TH2D *AB_panel = new TH2D("AB_panel", ...);

Thanks! I should also add #include "TROOT.h" in my header.