-
I need help to find the memory filler...
I have an example where i use a function returning a Collection object.
Code:
Public Sub Main()
Dim myColl As Collection
Set myColl = getColl2
MsgBox myColl.Item(1)
End Sub
Function getColl() As Collection
Set getColl = New Collection
getColl.Add "something"
End Function
Function getColl2() As Collection
Dim A As New Collection
A.Add "something"
Set getColl2 = A
Set A = Nothing
End Function
Both functions will return the same object, but when will the object cease to exist in memory?
I am experiencing out-of-memory problems with my code.
My understanding of SET and NEW:
Using SET actually sets a pointer to the object. It doesn't copy its contents. Using the NEW creates a new instance of the object and the SET sets a pointer to it.
Will VBs internal garbage can take care of the object when it is no longer pointed at, or will it stay there until the program exits?
The function getColl() creates an instance of the Collection object, but will the object ever get removed from memory, when it never gets a getColl2 = nothing ?
Anyone know an ultimate routine of some sort that can garbage anything created from the current program and that is no longer in use in memory?
I need to find the memory-filler in my code, and the only uncertain factors are the Collection object described above, and the use of the CopyMemory API:
Code:
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(lpvDest As Any, _
lpvSource As Any, _
ByVal cbCopy As Long)
Sub MySub()
Dim b(1 To 10) As Byte
CopyMemory b(1), ByVal StringPointerFromAnAPI, 10
For i = 1 To 10
Debug.Print Chr(b(i)); 'Prints the string to debug.
Next
End Function
-
Here's how i see it.
All object variables are references to the objects, if you
1. use set to set nothing or to change the contents of a object variable
2. exit a procedure containing with a object variable in procedure scope
3. unload an object containing that object variable
4. end your app
Then it will unload the object from memory
There's no way to copy a object in vb except if you make a function inside the class that can transfer the data between objects
New is used to create new instances yes.
Getcoll2 will always return a new collection with "something", not the same collection. You could check it out with Objptr()