GetOwnership()?

It would be useful to know if PyROOT thinks python should own a particular ROOT object. Is it possible to obtain such information (even through a hack?) I need it for debugging a memory problem.

Cheers,

  • Pete

For that matter, are there any good tools for determining where objects are being leaked from within python?

In principle, I presume it should not be too difficult with sys.setprofile()/settrace() and ROOT’s memory watching system?

  • Pete

Pete,

a hack …[code]#include <sys/types.h> // for ssize_t

struct _object;

struct TFakeObjectProxy {
ssize_t fRefCnt; // PyObject_HEAD
void* fPyType; // PyObject_HEAD
void* fRootObj;
int fFlags;
};

bool GetOwnership( _object* obj ) {
return ((TFakeObjectProxy*)obj)->fFlags & 0x0001;
}[/code]

[code]>>> from ROOT import *

gROOT.LoadMacro( “hack.C++” )
Info in TUnixSystem::ACLiC: creating shared library ./hack_C.so
0

a = TObject()
GetOwnership( a )
1

SetOwnership( a, False )
GetOwnership( a )
0

[/code]

Cheers,
Wim

Thanks wlav! This will be useful. :smiley:

  • Pete

Can an official, less “hackish” version of GetOwnership find its way into PyROOT?

I am thinking of usage along the lines of:

[code]from future import with_statement
from ROOT import SetOwnership, GetOwnership, TH1D

class PreserveOwnership(object):
def init(self, myObject):
self.myObject = myObject
def enter(self):
self.OldOwnership = GetOwnership()
def exit(self, tp, va, tr):
SetOwnership(self.myObject, self.OldOwnership)

myHist = TH1D()

with PreserveOwnership(myHist):
ROOTFunctionWhichHasNonConstArgs(myHist)

Python ownership of myHist restored to original state after with block

[/code]

Maybe there is another way of achieving this?

Hi,

only for individual functions or globally? There is the setting not to use memory heuristics, but to keep the code strict. In that case, no ownership is lost (globally) unless explicitly released.

Can’t do it yet for individual functions (is on wishlist; functions can be decorate to whether their return result is python-owned or not).

Cheers,
Wim

Hmm, I agree changing the memory policy might be one way of achieving similar results. But on the whole, the heuristic memory policy is good, it would be nice if it was possible to specify that just particular objects should retain whatever their python ownership was before a given set of function calls.

Hi,

it’s a little convoluted, but a prototype is in HEAD SVN: use of the variable _mempolicy (not set, or set to either kMemoryStrict or kMemoryStrict) on functions directs the individual behavior of that function, overriding global behavior.

Cheers,
Wim