Results 1 to 2 of 2

Thread: Need help finding the memory filler.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Unhappy

    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

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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()
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width