Consider the following:
from TestMod import Base
class Another:
def __init__ (self):
print "Initializing Another"
self.Funky = None
def __del__ (self):
self.Funky = None
print "Finalizing Another"
class Derived (Base):
def __init__ (self):
Base.__init__ (self)
print "Initializing Derived"
self.another = Another ()
self.another.Funky = self.Func
def __del__ (self):
self.another = None
print "Finalizing Derived"
def Func (self):
print "Funky!"
Base is a C++ class imported into Python via Boost.
When Derived gives Another a reference to it Func, it also provides a reference to itself. As a result, when I destroy the reference to Derived in C++, the destructor is not called because there is still one reference remaining: in Another. Both reference each other and there are no other references beyond those. When I call Py_Finalize, Base's destructor is not being called.
Is there a way to simply tell Python to clear all references and destroy all objects, calling their destructors? Right now, I have to clear all the references manually.
from TestMod import Base
class Another:
def __init__ (self):
print "Initializing Another"
self.Funky = None
def __del__ (self):
self.Funky = None
print "Finalizing Another"
class Derived (Base):
def __init__ (self):
Base.__init__ (self)
print "Initializing Derived"
self.another = Another ()
self.another.Funky = self.Func
def __del__ (self):
self.Destroy ()
print "Finalizing Derived"
def Destroy (self):
self.another = None
def Func (self):
print "Funky!"
I call the Destroy function in Derived before I delete its C++ reference. This will guarantee that the reference to Another is deleted and its destructor called. However, if there was a similar case with Another (where there was a reference to it an an object that contained a reference back to Another), it would also have to have a Destroy function and Derived would have to call it.
I don't know whether or not this would be a problem for straight Python code. I don't know if Python full manages its memory and destroys all objects created. What I do know is that Py_Finalize isn't calling C++ destructors, which can lead to other issues. My own solution isn't -too- much trouble, but I'd like to know if there is something I've missed. Is there, for example, a function in Python's C-API to destroy everything and ensure destructors are properly called?