• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1573
  • Last Modified:

How to check for memory leaks in VB6

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
ASP_Ray
Asked:
ASP_Ray
1 Solution
 
gencrossCommented:
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
 
gencrossCommented:
BTW...

You should be able to get a lot of hits searching the net on "Bounds Checker"
0
 
CrinCommented:
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.

 
EDDYKTCommented:
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
 
EDDYKTCommented:
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
 
mlmccCommented:
learning
0
 
corvanderlindenCommented:
I would recommend SmartCheck from Numega (www.compuware.com)
0
 
ASP_RayAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now