Why is canvas empty when I get graph from TObjArray?

Hello!

I do not understand why I do not see graph.

void DrawCanvas()
{
    double xv[5] = {1, 2, 3, 4, 5};
    double yv[5] = {11, 12, 13, 14, 15};
    TGraph* gr_1 = new TGraph(5, xv, yv);

    //put gr_1 in array
    TObjArray obj_array(0);
    obj_array.SetOwner(kTRUE);
    obj_array.Add( gr_1->Clone() );

    //get gr_1 from array
    TGraph* gr_2 = obj_array[0];

    //all work correctly
    cout << "gr_2->GetN() = " << gr_2->GetN() << endl;
    for(int j = 0; j < 5; j++)
    {
        double x, y;
        gr_2->GetPoint(j, x, y);
        cout << j << " " << x << " " << y << endl;
    }

    TCanvas *canv = new TCanvas("c123", "c456", 0, 0, 1900, 1500);
//    gr_1->Draw("apl");//I see graph
    gr_2->Draw("apl");//Why I do not see graph?

    canv->ls();
}

gr_2 has data.
Output of DrawCanvas()

gr_2->GetN() = 5
0 1 11
1 2 12
2 3 13
3 4 14
4 5 15

What is wrong?

Thank you in advance.
Best regards, Vladislav.

Hi Vladislav,

it’s an ownership issue.
In the case of the first graph, gr_1, it survives the scope where it is created and the Canvas properly visualise it. While for the second graph, the TObjArray owns it and the container goes out of scope at the end of the macro. You can solve this invoking DrawClone rather than Draw.

Cheers,
D

Thanks!

obj_array.SetOwner(kTRUE);
gr_2->DrawClone("apl");

or

//    obj_array.SetOwner(kTRUE);
gr_2->Draw("apl");

works.