Re: Memory Leak in VB6 with strings and UDTs

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thank you.

OK, I tried to clarify my question, and to simplify my code (I left the
entire code at the end off and just included snippets of the important
stuff). Let me try again

I have found what I think is a memory leak at the core of Visual Basic 6,
either that or something I don’t understand.

I cannot list my exact code because of security so have created a simple
example that mocks what we do. I show some excerpts from that below. I have
a UDT array, myudt, which contains another UDT array, ABtype, which has a
string field called BString1.

When, in a loop over my arrays, I set BString1 to an element of an array,
bytearray, which = 111, this seems to cause a memory leak. I believe there
is a memory leak because the MS perfmonitor shows “Private Bytes” rising
about 5 MB overnight.

I have tried casting the number to a string with CStr but that doesn't help
at all. I think making the string field an integer, or a fixed length string
prevents the problem, but does anybody know why this happens. It seems like a
memory leak within the core of VB 6.
If I change BString to an integer the leak goes away.

--------------------

Here are the important definitions (from a form)
Private Type B
‘ Using this defn as string leaks.
BString1 As String

' This does NOT leak
‘’’’’BString1 As Integer
' This does NOT leak
'’’’’BString1 As String * 5
End Type

Private Type myUDTA
ABtype(1 To 1000) As B
End Type


Const MSIZE As Long = 1000
Dim myudt(1 To 1000) As myUDTA
Dim bytearray(1 To 1000) As Byte

The field of concern is the BString1 above in the B UDT.

In a timer I call the following:
....
Call f1(bytearray)
End Sub

bytearray is an array of bytes all = 111

Public Function f1(ba() As Byte) As Boolean
Dim i As Integer
Dim k As Integer
Dim tempstr As String

For i = 1 To MSIZE
For k = 1 To MSIZE
‘ It appears that the statement below causes memory to leak.
myudt(i).ABtype(k).BString1 = ba(i)

'''''''''''''tempstr = ba(i) ' This did not leak!!!!!
Next k
Next i
End Function


"Michael C" wrote:

"goodhart" <goodhart@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:506A3F88-0993-404A-8BD2-0CAB83F93296@xxxxxxxxxxxxxxxx
I am using VB6 in Visual Studio.

Our application seems to leak memory. I created a simple example to
illustrate the problem.

It basically involves setting a string field of a UDT within a UDT to some
number (a byte but the same has happened with ints). When I do that I
see
a memory leak over time. I have tried casting the number to a string with
CStr but that doesn't help at all. I think making the string field an
integer, or a fixed length string prevents the problem, but does anybody
know
why this happens. It seems like a memory leak within the core of VB 6.

You really need to simplify the sample code you posted.

Michael



.



Relevant Pages

  • Re: another memory leak, weird`
    ... when I input a few charactors, like 10 charactors, no memory leak ... The string 'a' has not yet gone out of scope and has not been destroyed ... the heap is reported as a leak. ...
    (microsoft.public.vc.language)
  • Re: Is how to lay out the constant string compiler dependent?
    ... memory of the string that you have used except store the address in a ... the memory as the general memory leak. ... a literal array that is modifiable cannot ...
    (comp.lang.c)
  • Dispose Unmanaged resources
    ... We all know that in .NET, we don't need to worry about memory leak ... illustrate the step necessary in disposing unmanaged resources. ... public static extern bool COMDbConnection (string connectionString, ...
    (microsoft.public.dotnet.general)
  • Re: ctypes: delay conversion from c_char_p to string
    ... way to do this with ctypes? ... # without giving me an opportunity to destroy the original string. ... this technique for getting pointer *is* mentioned in ctypes documentation. ... I believe this will cause a memory leak, ...
    (comp.lang.python)
  • Re: Access 2003 runs slow after 2-3 searches
    ... txtFindDriverName = "" 'so when the query runs it returns all drivers ... Dim rs As Recordset ... And this is the code in the query ... you may have a memory leak. ...
    (microsoft.public.access.forms)