Solved

How to check for memory leaks in VB6

Posted on 2002-05-28
8
1,514 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
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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

708 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