I have some inherited Xojo code that has many places where the Destructor() method of an object is called directly from code. I was under the impression that this is not something one should ever (need to) do. My question is: what is the effect of such a direct call? Does it dispose of the object, even if it has references to it. Does it dispose of the object only if it has no references, or only weak references to it?
No.
No.
An object gets disposed as soon as no reference is pointing to it. Then the destructor is called by the Xojo framework. Calling the destructor from code is bad practice but not harmful, it just executes the code in it.
Calling the Destructor directly is just like calling a normal method; the effect is to run whatever code lives in the Destructor, and nothing more. The object is not destroyed, its refcount is not changed, and superclass Destructors (if they exist) are not called automatically.
A lot of Destructors are probably not written to be called several times.
e.g. they release the underlaying C++ or OS object and accessing a property could cause a segmentation fault.
This is what I would assume. However, I’m seeing circumstantial evidence that there might be side effects. I’m going to experiment in a test app today.
I’d agree with Christian that destructors should probably not be called directly or multiple times
After running tests, I believe what you say is all true.
Fun fact: If your object’s Destructor gets called because there are no references to it any more, and if you then store a new reference to your object somewhere, the object won’t get freed. It keeps on living, although its subclass destructors may already have been called. Once you the remove all refs to that object again, all the destructors get called once again. To use Norman’s favorite phrase: “You should probably not rely on this” Though, if Xojo’s runtime would ever break this, I’d be in big trouble.
This one one can be relied on and treated as a language contract.
Yay, thanks for confirming, Joe. Without that, my Cocoa Scripting bridge for Xojo would not be possible.