Hi Jan Erik,
When you do:
leg1->AddEntry(_histLeptonMomLikeSign_All_Reco, "all", "f");
the TLegend object keeps a pointer to the object
_histLeptonMomLikeSign_All_Reco.
When the object is deleted, the TLegend object still points to the deleted
object. I have now protected this situation in the development version in CVS.
Rene Brun
Jan Erik Sundermann wrote:
>
> Hi rooters,
>
> i'm drawing some histograms into the same canvas. If i delete the drawn
> histograms and replace them with new ones (with the same names) it always
> ends up with a segementation violation. If i try to draw the canvas
> without the legend everything works fine.
>
> ... this is my code.
>
> void
> TMcBReco::drawHistoLeptonSpectra() {
>
> gStyle->SetOptStat(10);
>
> TCanvas* canvas = new TCanvas("LeptonSpectra","LeptonSpectra");
>
> canvas->cd();
> canvas->Divide(2,0);
>
> // like sign
> canvas->cd(1);
> _histLeptonMomLikeSign_All_Reco->SetXTitle("p / GeV");
> _histLeptonMomLikeSign_TruthMatch_Reco->SetFillColor(18);
> _histLeptonMomLikeSign_TruthMatch_Reco->SetFillStyle(1001);
> _histLeptonMomLikeSign_FromB_Reco->SetFillColor(1);
> _histLeptonMomLikeSign_FromB_Reco->SetFillStyle(3004);
> _histLeptonMomLikeSign_All_Reco->Draw();
> _histLeptonMomLikeSign_TruthMatch_Reco->Draw("SAME");
> _histLeptonMomLikeSign_FromB_Reco->Draw("SAME");
>
> TLegend* leg1 = new TLegend(0.5, 0.5, 0.85, 0.75);
> leg1->SetHeader("lepton momentum (like-sign)");
> leg1->AddEntry(_histLeptonMomLikeSign_All_Reco, "all", "f");
> leg1->AddEntry(_histLeptonMomLikeSign_TruthMatch_Reco, "with truth match", "f");
> leg1->AddEntry(_histLeptonMomLikeSign_FromB_Reco, "with truth match from b", "f");
> leg1->Draw();
>
> ( ... )
>
> }
>
> ----> Calling TMcBReco::histosLeptonSpectra a second time
> (after calling TMcBReco::drawHistoLeptonSpectra) allways causes
> a segmentation violation
>
> void
> TMcBReco::histosLeptonSpectra(bool CM = false) {
>
> removeObject("histLeptonMomLikeSign_All_Reco");
> _histLeptonMomLikeSign_All_Reco =
> new TH1D("histLeptonMomLikeSign_All_Reco","histLeptonMomLikeSign_All_Reco",
> _nbBins_mom,_left_mom,_right_mom);
>
> ( ... filling histograms )
>
> }
>
> void
> TMcBReco::removeObject(const char* const name) {
>
> TObject* obj = gDirectory->Get(name);
> if (obj != 0) {
> cout << "removing object " << name << endl;
> obj->Delete();
> }
> }
>
> What am i doing wrong.
>
> Best regards,
>
> Jan Erik Sundermann.
This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:46 MET