Problem with collection

Hello,
please help with this macro (attach). What is wrong ?

#include <TList.h>
#include <TH1.h>

TList *histograms = new TList();

TH1 *FindHis(const char *name) { return (TH1 *)histograms->FindObject(name); }

void HistoManager(TH1 *his, Option_t *option)
{
  if (!his) return;
  TString action = option;
  action.ToLower();

  if (action == "add") {
    TH1 *prev = FindHis(his->GetName()); // find previous histo with same name
    if (prev) {
      Printf("%s replaced", prev->GetName());
      delete prev;
    }
    his->SetDirectory(0);
    his->SetBit(kMustCleanup);
    histograms->Add(his);
  }
}

[quote]
root [0] .L problem.C+
Info in TUnixSystem::ACLiC: creating shared library /home/musinsky/./problem_C.so
root [1] TH1F *h1 = new TH1F(“nameA”, “”, 10, 0, 10)
root [2] HistoManager(h1, “add”)
root [3] TH1F *h2 = new TH1F(“nameA”, “”, 10, 0, 10)
root [4] HistoManager(h2, “add”)
nameA replaced
root [5] TH1F *h3 = new TH1F(“nameB”, “”, 10, 0, 10)
root [6] HistoManager(h3, “add”)
nameB replaced

*** Break *** segmentation violation
(no debugging symbols found)
[/quote] FC9, gcc version 4.1.2 20070925 (Red Hat 4.1.2-33), ROOT 5.19/02
Thanks, Jan
problem.C (547 Bytes)

Jan,

deleting prev is not enough, you must also remove it from your TlLst* histograms.

Rene

but object prev kMustCleanup bit is set => after deleting prev is remove (automatically) from list histograms.

I forgot: gROOT->GetListOfCleanups()->Add(histograms), solve this problem
Jan

Yes adding your list to the list of cleanups solve your problem, otherwise ROOT has no way to find out in which list(s) your histogram is referenced.

Rene