Link to home
Start Free TrialLog in
Avatar of ocsscott
ocsscott

asked on

Why does this code leak memory?

here is some code iv'e seen dozens of times to see if a program is running.  On win 9x/2k machines this code leaks memory badly.  it is used in a times loop every 2 seconds.  Im leaking 2 meg of ram per minute.  any ideas??

Function IsRunning(sTask As String) As Boolean

    Dim x&, ret&
    Dim sName$
    Dim Proc As PROCESSENTRY32
   
    ret = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
   
    If ret = 0 Then Exit Function
   
    Proc.dwSize = Len(Proc)
   
    x = Process32First(ret, Proc)
   
    Do While x
      sName = Left$(Proc.szexeFile, Len(Proc.szexeFile) - 1)
      sName = UCase(Trim$(Left$(sName, InStr(sName, Chr(0)) - 1)))

      If InStr(sName, UCase(sTask)) Then
         IsRunning = True
         Exit Function
      End If

      x = Process32Next(ret, Proc)
      DoEvents
    Loop
   
 End Function
Avatar of ocsscott
ocsscott

ASKER

the data types I forgot them
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

Public Const MAX_PATH = 260

Public Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szexeFile As String * MAX_PATH
End Type
I'd try clearing the UDT

Set Proc = Nothing
Dave.

That won't work either

You need to call

CloseToolhelp32Snapshot

This will close the handle ret to the snapshot object.

Public Declare Function CloseToolhelp32Snapshot Lib "kernel32" (ByVal hHandle
As Long) As Long

Vin.


ASKER CERTIFIED SOLUTION
Avatar of DennisL
DennisL

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is before you exit your function add the following code.


Function IsRunning(sTask As String) As Boolean

   Dim x&, ret&
   Dim sName$
   Dim Proc As PROCESSENTRY32
   
   ret = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
   
   If ret = 0 Then Exit Function
   
   Proc.dwSize = Len(Proc)
   
   x = Process32First(ret, Proc)
   
   Do While x
     sName = Left$(Proc.szexeFile, Len(Proc.szexeFile) - 1)
     sName = UCase(Trim$(Left$(sName, InStr(sName, Chr(0)) - 1)))

     If InStr(sName, UCase(sTask)) Then
        IsRunning = True
        Exit Function
     End If

     x = Process32Next(ret, Proc)
     DoEvents
   Loop

   'New line
   CloseToolhelp32Snapshot(ret)

End Function

Note: Attempting to call CloseHandle on ret will also result in a memory leak.

Vin.

Do not do what DennisL has suggested this will definately cause a memory leak.

Vin.
The method suggested by DennisL is exactly how Microsoft show to do it in the example App Modlist.exe.  Not that Microsoft hasn't been known to release examples with bugs in them or at the very least bad coding practices.  And they do it not once but twice.

Regards,

eeevans
Sorry, I meant to paste in the link for the example App:

http://download.microsoft.com/download/vb50pro/Sample7/1/W9XNT4/EN-US/Modlist.exe

Regards,
eeevans
If you read the help on this function it states:

-----------------------------------
Remarks
The snapshot returned is a copy of the current state of the system. To close a snapshot call the CloseToolhelp32Snapshot function. Do not call the CloseHandle function to close the snapshot call. Calling CloseHandle to close the snapshot call generates a memory leak.

The snapshot taken by this function is examined by the other tool help functions to provide their results. Access to the snapshot is read only. The snapshot handle acts like an object handle and is subject to the same rules regarding which processes and threads it is valid in.

To retrieve an extended error status code generated by this function, use the GetLastError function.
------------------------------------

Vin.
Leave it to Microsoft...I found this in the Jan 2001 MSDN Lib

----------------
CreateToolhelp32Snapshot
Takes a snapshot of the processes and the heaps, modules, and threads used by the processes.

HANDLE WINAPI CreateToolhelp32Snapshot(
  DWORD dwFlags,      
  DWORD th32ProcessID  
);
Parameters
dwFlags
[in] Specifies portions of the system to include in the snapshot. This parameter can be one of the following values. Value Meaning
TH32CS_INHERIT Indicates that the snapshot handle is to be inheritable.
TH32CS_SNAPALL Equivalent to specifying TH32CS_SNAPHEAPLIST, TH32CS_SNAPMODULE, TH32CS_SNAPPROCESS, and TH32CS_SNAPTHREAD.
TH32CS_SNAPHEAPLIST Includes the heap list of the specified process in the snapshot.
TH32CS_SNAPMODULE Includes the module list of the specified process in the snapshot.
TH32CS_SNAPPROCESS Includes the process list in the snapshot.
TH32CS_SNAPTHREAD Includes the thread list in the snapshot.


th32ProcessID
[in] Specifies the process identifier. This parameter can be zero to indicate the current process. This parameter is used when the TH32CS_SNAPHEAPLIST or TH32CS_SNAPMODULE value is specified. Otherwise, it is ignored.
Return Values
Returns an open handle to the specified snapshot if successful or ? 1 otherwise.

Remarks
The snapshot taken by this function is examined by the other tool help functions to provide their results. Access to the snapshot is read only. The snapshot handle acts like an object handle and is subject to the same rules regarding which processes and threads it is valid in.

To retrieve an extended error status code generated by this function, use the GetLastError function.

To destroy the snapshot, use the CloseHandle function.

Requirements
  Windows NT/2000: Requires Windows 2000.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Tlhelp32.h.
  Library: Use Kernel32.lib.

-------------------------------

Regards,
eeevans
My help must be all over the place then I had a look on another machine and found the same thing.

Ok call CloseHandle(ret) rather than CloseToolhelp32Snapshot

Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long

Sorry guys.

Vin.

 
Actually, Vin I trust the help you found a little more.  I think I would rather call ClostToolhelp32Snapshot as it sounds like it was taylor made to do just what is needed.  I just thought it funny to find yet another example where MS can't keep its story straight even in its own documentation.  If MS would really like to support the development community, it could start by documenting all of the OS quirks (read "features") hidden and otherwise and do it clearly and correctly.  I have found better documentation in some of the "Hidden Secrets of the API" type books than have ever come out of Redmond.

Regards,
eeevans
I had a look at the DLL exports table of Kernel32 this function is not supported on WINNT is is Windows CE apparently.

Why do the do this to us.

Again apologies guys.

Use the CloseHandle API to release the object you should see your memory leak disappear.

Vin.
<ping>
thanks all for all the help that took care of the leak.  dennis had it first but i'ld like to thank you all and especially vincent for all of the input and help

scott