Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
DonStauffer99
642 Views, 4 Replies

iLogic class destructor

I created a Finalize method in one of my classes. I instantiate the class in a parameter change rule, so it seems like it should go out of scope after the rule finishes, but the method never executes. Do destructors even work in iLogic classes? Is Finalize the right method name? Thanks!

Example: I put this code into a rule, and when it runs, the last message box says than n_ is 3. It seems like it should be zero at that point. The messages in the Finalize method never display. It seems like one or the other of them should display 3 times. I have a bigger project, and classes that unload files in their destructor. Those destructors never ran until I put a Save (to disk) function in my code before the objects go out of scope. Then the destructors mostly started running. Why would it be inconsistent like that?

SyntaxEditor Code Snippet

ublic Class ThisRule
	Public Sub Main()
		Dim a As New Counted
		Dim b As New Counted
		MsgBox("creating a")
		a = Nothing
		MsgBox("a destroyed")
		f()
		MsgBox("f returned")
		b = Nothing
		MsgBox("b destroyed")
		
		MsgBox("n=" & Counted.n_.ToString())
	End Sub
	
	Public Sub f()
		Dim c As New Counted
	End Sub
End Class

Public Class Counted
	Public Shared n_ As Integer = 0
	Public Sub New()
		n_ = n_ + 1
	End Sub
	Public Sub Finalize()
		n_ = n_ - 1
		If n_ = 0 Then
			MsgBox("Destroyed!")
		Else
			MsgBox("Not Destroyed")
		End if
	End Sub
End Class

No speculation, even? Confirmation this happens? Additional information about when it does or doesn't happen, or workarounds?

AlexFielder
in reply to: DonStauffer99

Hiya,

In my experience, in a "Try Catch Finally" scenario - the Finally always fires so you could use that instead?

DonStauffer99
in reply to: AlexFielder

That's an interesting idea. I did discover that Microsoft revised VB so it no longer has destructors! This may help work around. Thanks .for the suggestion.