Solved

Why does this code leak memory?

Posted on 2001-08-23
15
332 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now