Solved

Why does this code leak memory?

Posted on 2001-08-23
15
341 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:ocsscott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +3
15 Comments
 

Author Comment

by:ocsscott
ID: 6417993
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
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6418033
I'd try clearing the UDT

Set Proc = Nothing
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6418093
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.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
DennisL earned 100 total points
ID: 6418105
You're creating Objects and not releasing them from memory.


ret = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ' creating an object returning a handle (this is not being released)

' ////////////////////
' rest of code....
' ////////////////////

CloseHandle(ret) ' get rid of the object
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6418114
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.

0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6418125
Do not do what DennisL has suggested this will definately cause a memory leak.

Vin.
0
 
LVL 1

Expert Comment

by:eeevans
ID: 6418161
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
0
 
LVL 1

Expert Comment

by:eeevans
ID: 6418177
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
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6418187
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.
0
 
LVL 1

Expert Comment

by:eeevans
ID: 6418212
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
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6418229
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.

 
0
 
LVL 1

Expert Comment

by:eeevans
ID: 6418263
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
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6418290
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.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6418327
<ping>
0
 

Author Comment

by:ocsscott
ID: 6418770
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question