Objects in memory that get erased

Hi,

I tried to look for the answer to this question in the forum but I didn’t succeed.

I would like to make objects persist in memory even when their references are lost. For example, if I do, inside a function:

def myf(someHisto):

newHisto = someHisto.Clone()

I would then like to have the clone exist in memory after the code exits the call to the function, but I cannot manage to keep it alive. Is there any way this can be done?

Thanks!

Hi,

if the python side needs to manage the object, a reference needs to exist somewhere. If you rely on the C++ side to take over management, you can do:import ROOT ROOT.SetOwnership(newHisto, False)
The reason that python thinks it has ownership is b/c Clone() creates a new object. You can also tell it that Clone() does not create new objects (but make sure to manage them on the C++ side then, e.g. on a TCanvas), by globally setting:someHisto.__class__.Clone._creates = False
But again, to emphasize, that’s a global change for all calls to Clone() on that type of histo everywhere.

Cheers,
Wim