mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-05 03:15:07 -04:00
prevent garbage leak
This commit is contained in:
parent
93a5850c7b
commit
a86451ad35
@ -62,41 +62,47 @@ class EnforcesCalldowns:
|
|||||||
if EnforcesCalldowns.notActive():
|
if EnforcesCalldowns.notActive():
|
||||||
return
|
return
|
||||||
|
|
||||||
# this map tracks how many times each func has been called
|
# protect against multiple calldowns via multiple inheritance
|
||||||
self._funcId2calls = {}
|
if hasattr(self, '_numInits'):
|
||||||
# this map tracks the 'latch' values for each func; if the call count
|
self._numInits += 1
|
||||||
# for a func is greater than the latch, then the func has been called.
|
else:
|
||||||
self._funcId2latch = {}
|
self._numInits = 1
|
||||||
|
|
||||||
if self.__class__ not in EnforcesCalldowns._class2funcName2funcIds:
|
# this map tracks how many times each func has been called
|
||||||
# prepare stubs to enforce method call-downs
|
self._funcId2calls = {}
|
||||||
EnforcesCalldowns._class2funcName2funcIds.setdefault(self.__class__, {})
|
# this map tracks the 'latch' values for each func; if the call count
|
||||||
# look through all of our base classes and find matches
|
# for a func is greater than the latch, then the func has been called.
|
||||||
classes = ClassTree(self).getAllClasses()
|
self._funcId2latch = {}
|
||||||
# collect IDs of all the enforced methods
|
|
||||||
funcId2func = {}
|
if self.__class__ not in EnforcesCalldowns._class2funcName2funcIds:
|
||||||
for cls in classes:
|
# prepare stubs to enforce method call-downs
|
||||||
for name, item in cls.__dict__.items():
|
EnforcesCalldowns._class2funcName2funcIds.setdefault(self.__class__, {})
|
||||||
if id(item) in EnforcesCalldowns._decoId2funcId:
|
# look through all of our base classes and find matches
|
||||||
funcId = EnforcesCalldowns._decoId2funcId[id(item)]
|
classes = ClassTree(self).getAllClasses()
|
||||||
funcId2func[funcId] = item
|
# collect IDs of all the enforced methods
|
||||||
EnforcesCalldowns._funcId2class[funcId] = cls
|
funcId2func = {}
|
||||||
# add these funcs to the list for our class
|
for cls in classes:
|
||||||
|
for name, item in cls.__dict__.items():
|
||||||
|
if id(item) in EnforcesCalldowns._decoId2funcId:
|
||||||
|
funcId = EnforcesCalldowns._decoId2funcId[id(item)]
|
||||||
|
funcId2func[funcId] = item
|
||||||
|
EnforcesCalldowns._funcId2class[funcId] = cls
|
||||||
|
# add these funcs to the list for our class
|
||||||
|
funcName2funcIds = EnforcesCalldowns._class2funcName2funcIds[self.__class__]
|
||||||
|
for funcId, func in funcId2func.items():
|
||||||
|
funcName2funcIds.setdefault(func.__name__, [])
|
||||||
|
funcName2funcIds[func.__name__].append(funcId)
|
||||||
|
|
||||||
|
# now run through all the enforced funcs for this class and insert
|
||||||
|
# stub methods to do the enforcement
|
||||||
funcName2funcIds = EnforcesCalldowns._class2funcName2funcIds[self.__class__]
|
funcName2funcIds = EnforcesCalldowns._class2funcName2funcIds[self.__class__]
|
||||||
for funcId, func in funcId2func.items():
|
self._obscuredMethodNames = set()
|
||||||
funcName2funcIds.setdefault(func.__name__, [])
|
for name in funcName2funcIds:
|
||||||
funcName2funcIds[func.__name__].append(funcId)
|
oldMethod = getattr(self, name)
|
||||||
|
self._obscuredMethodNames.add(name)
|
||||||
# now run through all the enforced funcs for this class and insert
|
setattr(self, name, new.instancemethod(
|
||||||
# stub methods to do the enforcement
|
Functor(EnforcesCalldowns._enforceCalldowns, oldMethod, name),
|
||||||
funcName2funcIds = EnforcesCalldowns._class2funcName2funcIds[self.__class__]
|
self, self.__class__))
|
||||||
self._obscuredMethodNames = set()
|
|
||||||
for name in funcName2funcIds:
|
|
||||||
oldMethod = getattr(self, name)
|
|
||||||
self._obscuredMethodNames.add(name)
|
|
||||||
setattr(self, name, new.instancemethod(
|
|
||||||
Functor(EnforcesCalldowns._enforceCalldowns, oldMethod, name),
|
|
||||||
self, self.__class__))
|
|
||||||
|
|
||||||
def EC_destroy(self):
|
def EC_destroy(self):
|
||||||
"""this used to be called destroy() but it was masking destroy() functions
|
"""this used to be called destroy() but it was masking destroy() functions
|
||||||
@ -105,12 +111,21 @@ class EnforcesCalldowns:
|
|||||||
if EnforcesCalldowns.notActive():
|
if EnforcesCalldowns.notActive():
|
||||||
return
|
return
|
||||||
# this must be called on destruction to prevent memory leaks
|
# this must be called on destruction to prevent memory leaks
|
||||||
for name in self._obscuredMethodNames:
|
# protect against multiple calldowns via multiple inheritance
|
||||||
delattr(self, name)
|
assert hasattr(self, '_numInits'), (
|
||||||
del self._obscuredMethodNames
|
'too many calls to EnforcesCalldowns.EC_destroy, class=%s' % self.__class__.__name__)
|
||||||
# this opens up more cans of worms. Let's keep it closed for the moment
|
if self._numInits == 1:
|
||||||
#del self._funcId2calls
|
for name in self._obscuredMethodNames:
|
||||||
#del self._funcId2latch
|
# Functors need to be destroyed to prevent garbage leaks
|
||||||
|
getattr(self, name).destroy()
|
||||||
|
delattr(self, name)
|
||||||
|
del self._obscuredMethodNames
|
||||||
|
# this opens up more cans of worms. Let's keep it closed for the moment
|
||||||
|
#del self._funcId2calls
|
||||||
|
#del self._funcId2latch
|
||||||
|
del self._numInits
|
||||||
|
else:
|
||||||
|
self._numInits -= 1
|
||||||
|
|
||||||
def skipCalldown(self, method):
|
def skipCalldown(self, method):
|
||||||
if EnforcesCalldowns.notActive():
|
if EnforcesCalldowns.notActive():
|
||||||
@ -164,15 +179,35 @@ if not EnforcesCalldowns.notActive():
|
|||||||
class CalldownEnforceTestSubclass(CalldownEnforceTest):
|
class CalldownEnforceTestSubclass(CalldownEnforceTest):
|
||||||
def testFunc(self):
|
def testFunc(self):
|
||||||
CalldownEnforceTest.testFunc(self)
|
CalldownEnforceTest.testFunc(self)
|
||||||
|
def destroy(self):
|
||||||
|
CalldownEnforceTest.EC_destroy(self)
|
||||||
class CalldownEnforceTestSubclassFail(CalldownEnforceTest):
|
class CalldownEnforceTestSubclassFail(CalldownEnforceTest):
|
||||||
def testFunc(self):
|
def testFunc(self):
|
||||||
pass
|
pass
|
||||||
class CalldownEnforceTestSubclassSkip(CalldownEnforceTest):
|
class CalldownEnforceTestSubclassSkip(CalldownEnforceTest):
|
||||||
def testFunc(self):
|
def testFunc(self):
|
||||||
self.skipCalldown(CalldownEnforceTest.testFunc)
|
self.skipCalldown(CalldownEnforceTest.testFunc)
|
||||||
|
class CalldownEnforceTestSubclass2(CalldownEnforceTest):
|
||||||
|
def testFunc(self):
|
||||||
|
CalldownEnforceTest.testFunc(self)
|
||||||
|
def destroy(self):
|
||||||
|
CalldownEnforceTest.EC_destroy(self)
|
||||||
|
class CalldownEnforceTestDiamond(CalldownEnforceTestSubclass,
|
||||||
|
CalldownEnforceTestSubclass2):
|
||||||
|
def __init__(self):
|
||||||
|
CalldownEnforceTestSubclass.__init__(self)
|
||||||
|
CalldownEnforceTestSubclass2.__init__(self)
|
||||||
|
def testFunc(self):
|
||||||
|
CalldownEnforceTestSubclass.testFunc(self)
|
||||||
|
CalldownEnforceTestSubclass2.testFunc(self)
|
||||||
|
def destroy(self):
|
||||||
|
CalldownEnforceTestSubclass.destroy(self)
|
||||||
|
CalldownEnforceTestSubclass2.destroy(self)
|
||||||
|
|
||||||
cets = CalldownEnforceTestSubclass()
|
cets = CalldownEnforceTestSubclass()
|
||||||
cetsf = CalldownEnforceTestSubclassFail()
|
cetsf = CalldownEnforceTestSubclassFail()
|
||||||
cetss = CalldownEnforceTestSubclassSkip()
|
cetss = CalldownEnforceTestSubclassSkip()
|
||||||
|
cetd = CalldownEnforceTestDiamond()
|
||||||
raised = False
|
raised = False
|
||||||
try:
|
try:
|
||||||
cets.testFunc()
|
cets.testFunc()
|
||||||
@ -194,12 +229,26 @@ if not EnforcesCalldowns.notActive():
|
|||||||
raised = True
|
raised = True
|
||||||
if raised:
|
if raised:
|
||||||
raise "calldownEnforced.skipCalldown raised when it shouldn't"
|
raise "calldownEnforced.skipCalldown raised when it shouldn't"
|
||||||
|
raised = False
|
||||||
|
cetd.testFunc()
|
||||||
|
msg = ''
|
||||||
|
try:
|
||||||
|
# make sure we're OK to call down to destroy multiple times
|
||||||
|
cetd.destroy()
|
||||||
|
except Exception, e:
|
||||||
|
msg = str(e)
|
||||||
|
raised = True
|
||||||
|
if raised:
|
||||||
|
raise "calldownEnforcedDiamond.destroy raised when it shouldn't\n%s" % msg
|
||||||
cetss.EC_destroy()
|
cetss.EC_destroy()
|
||||||
cetsf.EC_destroy()
|
cetsf.EC_destroy()
|
||||||
cets.EC_destroy()
|
cets.EC_destroy()
|
||||||
|
del cetd
|
||||||
del cetss
|
del cetss
|
||||||
del cetsf
|
del cetsf
|
||||||
del cets
|
del cets
|
||||||
|
del CalldownEnforceTestDiamond
|
||||||
|
del CalldownEnforceTestSubclass2
|
||||||
del CalldownEnforceTestSubclassSkip
|
del CalldownEnforceTestSubclassSkip
|
||||||
del CalldownEnforceTestSubclassFail
|
del CalldownEnforceTestSubclassFail
|
||||||
del CalldownEnforceTestSubclass
|
del CalldownEnforceTestSubclass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user