Is there a way to determine if a class instance has been destroyed?
tester1101 opened this issue · comments
Is there a way to write code like this?
class TestClass
function test1()
let a = new TestClass()
destroy a
if isDestroyed(a)
print("a is destroyed")
doSomething1()
else
print("a is alive")
doSomething2()
There is no reliable way to do this, because object ids are recycled. Thus your references to a destroyed object may turn into a valid reference to a new object which has been created afterwards. Hence checking for "destroyedness" is an anti-pattern.
What you should do is cleanup all references of the object when you destroy it, so that there are no references to the destroyed object. In many cases you can avoid having to clear a bunch of references by using the observer pattern.
I understand. I need to study the observer pattern. Thanks for the reply.