My VB6 application continues to run indefinitely as a process after the application closes. The phantom process uses 0 cpu but it takes up memory and ties up the .exe file as being in-use. The task manager doesn't show it listed as applications but it does show it in the process list.
I added "EndProcess(function shown below) programname.exe" at the start of the program to kill any processes leftover from any previous executions. Prior to that line of code, more and more phantom processes would appear in the process list each time the program ran. This line of code prevents the accumulation of phantom processes but I still can't get rid of the process that remains after the program finishes. This problem only occurs when running the .exe. When I run it in the IDE, it doesn't leave any processes running after it ends.
It looks to me through tracing in the IDE and msgboxes that all of the forms are being unloaded. Shouldn't that clear out all of the memory and kill the application and process when the final form is unloaded? I dynamically create a few controls while the program is running, are they removed from memory when the form is unloaded? I also define some variables as public in a module. Are the module's variables removed from memory when the final form is unloaded? How can I determine what is remaining resident in memory after the program runs?
Public Function EndProcess(ByVal sExeName As String) As Boolean
' This works at the beginning of the program to end any processes that are still running since the last time this program ran
On Error GoTo Err_Handler
Dim oProcessCollection, oProcess, ok, ii
Set oProcessCollection = GetObject("WinMgmts:").ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & sExeName & "'")
ii = oProcessCollection.Count
Debug.Print "Total: " & ii
If oProcessCollection.Count > 1 Then
For Each oProcess In oProcessCollection
ok = (oProcess.Terminate = 0)
If ok Then
ii = ii - 1
Debug.Print "Count: " & ii
If ii = 1 Then
EndProcess = False