Hi rooters,
I am trying to write some graphs to a file and I am getting the error
malloc: *** error for object 0x31ab094: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
The source of the error is in the following member function of a graphing class which takes a vector of shared pointers of TMultiGraphs and saves them to a root file:
void CGrapher::Save_plots(char TFilename[50]){
TFile *file = new TFile(TFilename,"UPDATE");
for(int i=0; i<n_gr; i++){
my1_gr[i].get()->SetMarkerStyle(7);
my2_gr[i].get()->SetMarkerStyle(7);
their_gr[i].get()->SetMarkerStyle(7);
my1_gr[i].get()->SetMarkerColor(kRed);
my2_gr[i].get()->SetMarkerColor(kBlue);
their_gr[i].get()->SetMarkerColor(kGreen);
multi_gr[i].get()->Add(my1_gr[i].get());
multi_gr[i].get()->Add(my2_gr[i].get());
multi_gr[i].get()->Add(their_gr[i].get());
multi_gr[i].get()->Write();
}
file->Close();
file->Delete();
}
ANy help would be much appreciated
cheers
Mark
Try: // "(TGraph*)" or "(TGraphErrors*)" or "(TGraphAsymmErrors*)" or ...
multi_gr[i].get()->Add(((TGraph*)(my1_gr[i].get()->Clone())));
multi_gr[i].get()->Add(((TGraph*)(my2_gr[i].get()->Clone())));
multi_gr[i].get()->Add(((TGraph*)(their_gr[i].get()->Clone()));
Also, instead of “file->Close(); file->Delete();”, you can simply say “delete file;”.
Note that using “Clone()” means doubling the RAM usage.
There’s an “alternative” solution, I believe.
Do not use “Clone()” in your “CGrapher::Save_plots” method, but in another method, right before you delete “multi_gr[…]” (or in any other place when you are sure they will not be used any more) add:
for (int i = 0; i < n_gr; i++)
if ((multi_gr[i].get()) && (multi_gr[i].get()->GetListOfGraphs()))
multi_gr[i].get()->GetListOfGraphs()->Clear("nodelete");
Hi thanks for that,
I did realise that Clone() would double the ram usage but I was hoping that when the smart pointer was destroyed so would the allocated memory… apparently not.
So just to clarify why should I not use" Clone()" in the “CGrapher::Save_plots” method, or more specifically whats the difference between “Clone()” being in “CGrapher::Save_plots” and some other method
and would it be okay to place
[quote] for (int i = 0; i < n_gr; i++)
if ((multi_gr[i].get()) && (multi_gr[i].get()->GetListOfGraphs()))
multi_gr[i].get()->GetListOfGraphs()->Clear("nodelete");
[/quote]
in the destructor?
cheers
Mark
Well, I don’t know where you delete your “multi_gr[…]”, possible in the destructor of your “CGrapher” class, so add these Clear(“nodelete”) calls there (just remember that they must come before you actually delete “multi_gr[…]”).