Crashes at end of loop (probably c++) problem

I doubt this is a root problem per se, but could someone explain my stupid c++ mistake. The crash occurs when the GetHistogramNew() called (not when it’s commented out). I thought it might be a pointer being overwriten, or not deleted, or something but I thought tried everything. And if you want to criticize my badly written c++, please do, I’m trying to learn.

void stackAllSigAndBack(TTree *_signal=signal, TTree *_ccbar=ccbar, TTree *_uds=uds, TTree *_bpbm=bpbm, TTree *_b0b0=b0b0, TCut cut=""){
  int count =0;
  TBranch *variable;
  TIter iter(_ccbar->GetListOfBranches());
  while(variable = (TBranch *)iter.Next()) {
  TCanvas *mycanvas = new TCanvas();
    mycanvas->cd();

    TString varname = variable->GetName();

    ccbar->Draw(varname);
    TAxis *xaxis = htemp->GetXaxis();
    int nbins = xaxis->GetNbins();
    double xmin = xaxis->GetXmin();
    double xmax = xaxis->GetXmax();

    THStack *hs = new THStack("hs", "Stack of Background");
    bpbmhist = GetHistogramNew(varname, bpbm, "bpbmhist", cut);
  }
}

where GetHistogramnew is


TH1F *GetHistogramNew(TString variableText, TTree *_tree, TString name, TCut cut=""){

  TString histoName = "h" + variableText + "_" + name;
  TString tempHisto = variableText + ">>" + histoName;
  _tree->Draw(tempHisto, cut);
  TH1F *histo = new TH1F();
  histo = (TH1F*) gDirectory->Get(histoName);

  if(!(histo)){
    cout << variableText << " histogram doesn't exist!" << endl;
    continue;
  }
  return histo;
}

Thanks
TSC

Replace:

TH1F *histo = new TH1F(); histo = (TH1F*) gDirectory->Get(histoName); by

TH1F *histo = (TH1F*) gDirectory->Get(histoName);
The TH1F object is created by gDirectory->Get.

Rene

Thanks Rene. I found this out after hours of debugging. Thanks again.
TSC