Memory leak

I find that the macro below has a memory leak. I am confused because I thought python should be garbage collected. I am using ROOT version 5.34/20

import ROOT
x1=ROOT.RooRealVar("x1","x1",1,0,10)
c1=ROOT.RooRealVar("c1","c1",-1,-1-0.1,-1+0.1)
rooexp1=ROOT.RooExponential("rooexp1","rooexp1",x1,c1) 
for i in range(0,300000):
    data1=rooexp1.generate(ROOT.RooArgSet(x1),1)    

Hi,

Python is garbage collected, but only for Python objects and by extension all C++ objects that those Python objects control. For C++ objects that are returned by pointer, and therefore are unclear in their ownership, the assumption is that C++ will take care of the memory management.

You can adjust that for individual methods, though. For example here, to make sure that Python takes ownership of the C++ object bound with ‘data1’, set the _creates member of the generate function:ROOT.RooExponential.generate._creates = True
Cheers,
Wim