Potential memory leak in TH1::Fit?

I am using mallocdebug on Mac OS X 10.4.10 / ROOT 5.14 in order to seek the remaining memory leaks in my code.

mallocdebug reports that I am almost systematically leaking 20 bytes of memory on calls to TH1I::Fit, an operation which I am doing very frequently.

I have narrowed down the offending code to a small testcase:

#include "TH1.h"
#include "TF1.h"
#include "TCanvas.h"
#include "TApplication.h"

int main(int argc, char** argv) {
	TApplication app("app", &argc, argv);
	TH1I *hist = new TH1I("histogram","histogram",11,0,11);
	TF1* fitfunc;
	fitfunc = new TF1("fitfunc","gaus", 3, 7);
	fitfunc->SetParameter(1, 5);
	hist->Fit("fitfunc", "R0Q");
	//TCanvas* canvas = new TCanvas();


This is particularly weird. When the call to TCanvas is commented, the leak does not occur. When it is uncommented, the leak occurs. mallocdebug traces the leaked buffer back to TH1::Fit(TF1*, char const*, char const*, double, double) -> TF1::Copy(TObject&) -> TFormula::Copy(TObject&) -> operator new[] -> operator new.

Could this be a erroneous report from mallocdebug caused by an incorrect use of the tool, which seems more tailored to find leaks in Apple Obj-C frameworks ?

Thanks a lot for your help.

Are you calling “new TCanvas” in a loop?
I do not see any leaks with the code that you posted (with or without
the new TCanvas).


Thanks to your reply.

Just the code snippet posted above reproduces it, without a loop.
This leads me to believe this is more related to the tool than anything else. I will try to reproduce this on other debugging environments or platforms.

I have narrowed down my problem. The “TCanvas glitch” was caused by the mallocdebug tool, which for a reason or another seems to be able to catch the memory leak only when a window is drawn. However, the leak was real:

while (true) {
TH1I hist("histogram","histogram",11,0,11);
TF1 fitfunc("fitfunc","gaus",5,7);

This script is sufficient to make the memory use skyrocket.

However, all of this was with ROOT 5.14. The good news is that the leak seems to be fixed in ROOT 5.16.