Clearing a TLegend from a canvas


I have a macro that generates a TLegend object on a canvas. The legend is stored as part of a larger class, and the class function to draw the legend is:

void SingleHistList::AddLegend(Float_t LowLeftX, Float_t LowLeftY, Float_t UpRightX, Float_t UpRightY)
  _Leg = new TLegend(LowLeftX, LowLeftY, UpRightX, UpRightY);

  for (Int_t i=0; i<_NHist; i++)
      _Leg->AddEntry(_Hists[i]->GetHist(), _Hists[i]->GetLegEntry());

When I call this function multiple times to resize the legend, the previous legend is not removed, so I end up with multiple boxes on the histogram. I have tried the Clear command, but this only removes the entry, not the TPave box itself. How can I remove this box?

Thanks very much,

When you do _Leg->Draw(), it adds a reference in the Pad list. So, several Draw() will cumulate the TLegends in the list and you will get many of them. Before creating a new one you should delete the previous TLegend if you do not need it anymore. You can use _Leg->Delete();
Or do the Draw() only when the AddEntry are finished. It depends what you want to do.