Best way of a class killing itself?


I have a console app that uses many classes. In one of them, following the class completing its work, I’d like it to kill itself.

What is the best way to do that?

Currently I am trying calling the Destructor Method.

Appreciate feedback.

Thank you,

Tell us more about what’s going on functionally. If you’re working on that request queue we were talking about recently, I have design suggestions I can offer.

Otherwise, you need to nil all references to the object for its destructor to occur. You can’t make that happen from within the instance.

Hi Tim,

Yes, this is for that same project.
I have a timer object in ClassA. It does stuff but when necessary it creates a new instance of the URLConnection object

TwilioSocket = New clsTwilioSocket

So, it creates a new instance then sends a single piece of info to the new Twilio Socket. I think I subclassed this properly, but maybe not… It sends that single piece of data separately since I cannot get access to the objects Constructor since it is taken by the URLConnection.

The new class is called clsTwilioSocket, with a Super of URLConnection.
Once the work is done in this instance, I could not think of a method to tell the creating object that it is done and can be removed. Although while writing this, I could send a message to a public method telling it there…

That’s were I thought of calling the clsTwilioSockets Destructor method.

I look forward to reading your thoughts!

Thank you,

So here’s an idea that might work for you.

You could make a private shared property on the class like this:

Private mInstances() as clsTwilioSocket

Make the class’ Constructor private and then make two shared methods for creating and destroying instances like this:

Shared Function Create()
  Dim cls as new clsTwilioSocket
  Return cls
End Function

Shared sub Destroy(inst as clsTwilioSocket)
  Dim idx as integer = mInstances.IndexOf(inst)
  If idx > -1 then
  End if
End Sub

If this was the only place the instances were held permanently, calling

clsTwilioSocket.destroy(item) would effectively destroy it

1 Like

Thanks Greg!

Appreciate your sample and explanations!


As you’ve discovered, you can’t call a Destructor to destroy an object. The Xojo way of doing this means you have to remove all references to the object, and then Xojo itself will “notice” that it can be destroyed and take care of it.

So as long as you keep track of all the references to the object and set then all to nil once the object is no longer needed, you’ll get exactly what you’re looking for. Your notion of the instance sending a “I’m finished!” message to the creating object is a good one. The creating object can respond to the message by removing any reference to the instance and thus triggering its destruction.

Another way to accomplish this is for the object to keep a reference to itself:


…and simply don’t keep any other references to it. Once your instance is finished, setting that self reference to nil will trigger its destruction:


Note that this approach is only a good idea if the rest of the code never needs to interact with the instance once it starts working, because you won’t be able to refer to it without holding an external reference to it. This technique is one form of a circular reference and while useful, it can lead to memory leakage if not used properly.