Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a very simple app that looks for a set of running executables and if they are running, it sets a registry value. The only problem is that it seems to be leaking memory at about 4 KB every 20 to 30 seconds. I am destroying the objects properly as far as I can tell. Going to post the entire codebase since it really is that small. Please keep in mind that Timer1 is set to 10 seconds which as you can see then calls sub Main. Any assistance is greatly appreciated.

Option Explicit

Private Sub Form_Load()
Form1.Visible = False
SetRegKey (2)
End Sub

Private Sub Timer1_Timer()
Main
End Sub

Sub Main()
On Error Resume Next

Dim objWMI
Dim strComputer
Dim colProcessList
Dim objProcess
Dim strProcess As String
Dim Present

strComputer = "."

Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

Set colProcessList = objWMI.ExecQuery("Select * from Win32_Process")

Dim arrProcess(1)
arrProcess(0) = "exe1.exe"
arrProcess(1) = "exe2.exe"
For Each Present In arrProcess
    For Each objProcess In colProcessList
        If LCase(objProcess.Name) = Present Then
            SetRegKey (0)
            GoTo GarbageCollect
        End If
    Next
Next

SetRegKey (2)

'Garbage Collect
GarbageCollect:
Set colProcessList = Nothing
Set objWMI = Nothing
End Sub

Function SetRegKey(dwValue)
On Error Resume Next

Dim objReg
Dim strComputer As String
Dim strKeyPath As String
Dim strValueName As String
Dim strdwValue

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Sample\Example\Server Common"
strValueName = "Connection Mode"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strdwValue

If strdwValue <> dwValue Then
    objReg.SetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue
End If

'Garbage Collect
Set objReg = Nothing
End Function
Posted
Updated 27-Sep-10 8:31am
v2

1 solution

Each time your timer fires you create a new psuedo thread, which goes and does its thing.

Are you sure that these processes are not being 'stuck' due to errors etc. and are actually terminating and disposing of the objects.

Also because you are just sweeping any errors under the carpet, there is no telling what is going on.

1) Before you Resume Next, write out the error message somewhere, so you can see if any problems arise.
2) Maintain a couple of counters, one for when the timer fires and starts the ball rolling, and one to count the number of times it exits its process and disposes of the objects. Monitor the 2 counters somewhere to see if they remain the same.

That will give you an idea of what is going on maybe.
 
Share this answer
 
Comments
DaveAuld 28-Sep-10 7:33am    
On Behalf of Doop2001: I tried that and nothing. Good suggestion, though. I did not think to isolate it like that. Can you, or anyone reading this for that matter, spot anything wrong with which objects I am destroying and how? Again, greatly appreciated.
Doop2001 4-Oct-10 16:12pm    
The leak was in the SetRegKey function. I can't explain because it looks like the objects are getting properly destroyed. I resolved it by only calling the function once either when one of the exes were running or when they terminated. It still leeks about 4 KB each time the function is called but much better than every 20 - 30 seconds.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900