Solved

How to check for memory leaks in VB6

Posted on 2002-05-28
8
1,539 Views
Last Modified: 2008-02-01
Can anyone tell me how to check for memory leaks in VB6?  According to VB documentation you should manually release the resources of all dynamically created variables with

  Set variable = Nothing

but what if I forget a few of these?  C++ has some nice diagnostic routines that report this kind of info, but what about VB6?  No book that I have ever seen on VB6 discusses this topic.  You can hardly find the word "memory" in the index of books on VB6.
0
Comment
Question by:ASP_Ray
[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
8 Comments
 
LVL 4

Accepted Solution

by:
gencross earned 100 total points
ID: 7039443
As far as I know the only way to do this is to purchase third party tools.  There is a nice one from NuMega called Bounds Checker.  I think it is somewhat expensive, but you may be able to find another on one of the download sites.

Hope this helps.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7039445
BTW...

You should be able to get a lot of hits searching the net on "Bounds Checker"
0
 
LVL 2

Expert Comment

by:Crin
ID: 7039658
I made it quite easy for me but with a little programming.

I defined global long variable lngObjCounter, in each class Initialize routine added
lngObjCounter = lngObjCounter + 1
Terminate routine:
lngObjCounter = lngObjCounter - 1

Actually, this helped a lot, but later I wrote own custom bound checker which write class name to some file with mark "+" on Initialize event and "-" on Terminate event.
Simple counting routine then helped me to find leaks.

Sincerely,
Crin


0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 26

Expert Comment

by:EDDYKT
ID: 7039940
I'm not sure that will help you


Private Type MEMORYSTATUS
    dwLength As Long
    dwMemoryLoad As Long
    dwTotalPhys As Long
    dwAvailPhys As Long
    dwTotalPageFile As Long
    dwAvailPageFile As Long
    dwTotalVirtual As Long
    dwAvailVirtual As Long
End Type
Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim MemStat As MEMORYSTATUS
    'retrieve the memory status
    GlobalMemoryStatus MemStat
    MsgBox "You have" + Str$(MemStat.dwTotalPhys / 1024) + " Kb total memory and" + Str$(MemStat.dwAvailPageFile / 1024) + " Kb available PageFile memory."
End Sub



or



Private Type LARGE_INTEGER
    LowPart As Long
    HighPart As Long
End Type
Private Type MEMORYSTATUSEX
    dwLength As Long
    dwMemoryLoad As Long
    ullTotalPhys As LARGE_INTEGER
    ullAvailPhys As LARGE_INTEGER
    ullTotalPageFile As LARGE_INTEGER
    ullAvailPageFile As LARGE_INTEGER
    ullTotalVirtual As LARGE_INTEGER
    ullAvailVirtual As LARGE_INTEGER
    ullAvailExtendedVirtual As LARGE_INTEGER
End Type
Private Declare Function GlobalMemoryStatusEx Lib "kernel32.dll" (ByRef lpBuffer As MEMORYSTATUSEX) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim MemStat As MEMORYSTATUSEX
    Dim TotalPhys As Currency
    'initialize structure
    MemStat.dwLength = Len(MemStat)
    'retireve memory information
    GlobalMemoryStatusEx MemStat
    'convert large integer to currency
    TotalPhys = LargeIntToCurrency(MemStat.ullTotalPhys)
    'show result
    MsgBox "Current memory load: " + CStr(MemStat.dwMemoryLoad) + vbCrLf + _
             "Total RAM: " + CStr(Int(TotalPhys / 1024 ^ 2)) + " Mb.", vbInformation
End Sub
Private Function LargeIntToCurrency(liInput As LARGE_INTEGER) As Currency
    'copy 8 bytes from the large integer to an empty currency
    CopyMemory LargeIntToCurrency, liInput, LenB(liInput)
    'adjust it
    LargeIntToCurrency = LargeIntToCurrency * 10000
End Function
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 7039949
Of course, you can alway use Performance monitor to monitor the memory leak on your task.

ie. on add counters dialog, select Process on Performance ocject and select private bytes on your task. Then add
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 7040307
learning
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 7041107
I would recommend SmartCheck from Numega (www.compuware.com)
0
 
LVL 1

Author Comment

by:ASP_Ray
ID: 7221492
A lot of really great info was given here but although I have not bought it, the recommendation that seems like it might really do what I want is SmartCheck.  The problem is it is sold only as part of a suite of development tools "DevPartner for Visual Basic".  I was told that "BoundsChecker" is a Visual C++ tool.  Thanks everybody.  Wish I could give points to all.
0

Featured Post

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!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

737 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