?
Solved

How to check for memory leaks in VB6

Posted on 2002-05-28
8
Medium Priority
?
1,550 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 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 101

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses

771 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