DrawClone of MultiGraph cause segmentation fault

Hi:

example:

#include<root/TCanvas.h>
#include<root/TMultiGraph.h>
#include<root/TGraph.h>
#include<root/TAxis.h>

auto graph1(TMultiGraph&graphs)
{
std::vectorpx1{-0.1, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
std::vectorpy1{-1,-2.9,-5.6,-7.4,-9,-9.6,-8.7,-6.3,-4.5,-1};
auto gr=new TGraph(px1.size(),px1.data(),py1.data());
gr->SetLineColor(kBlue);
graphs.Add(gr);
}

auto f()
{
TMultiGraph mg;
graph1(mg);
mg.DrawClone(“ac”);
mg.GetXaxis()->SetTitle(“X values”);
mg.GetYaxis()->SetTitle(“Y values”);
}

int main()
{
TCanvas canvas;
f();
canvas.Print(“haha.pdf”);
}

because I need to draw a very complex graph, I put the actual draw in the function f().
mg.DrawClone(“ac”); will cause segmentation fault. However, the drawclone of tgraph and tf1 just work ok. So how to use DrawClone of MultiGraph correctly?

Try: // ... #include "TPad.h" // ... void f(void) { TMultiGraph *mg = new TMultiGraph("mg", "All My Graphs;X values;Y values"); graph1(*mg); mg->Draw("ac"); gPad->Modified(); gPad->Update(); } // ...

That will cause memory leaking, mg->Draw just add pointer to the underlying pad. When leaving funciton f, no one actually delete the TMultiGraph. I know such way can do the job. But I want to find out a non memory leaking way to do that.

I am not dure I understand why DrawClone should avoid the memory leak Draw() is supposed to do…

I guess (s)he wants a “DrawCopy” behaviour.
Maybe it’s sufficient to add:
mg->SetBit(kCanDelete);
right before:
mg->Draw(“ac”);