Results 1 to 4 of 4

Thread: [RESOLVED] Erase on array of objects

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Resolved [RESOLVED] Erase on array of objects

    Since the Erase method is used to erase an array, does it clean up properly if it is an array of objects? Or do I have to iterate through the array and destroy each object separately?

    ex:

    vb Code:
    1. Option Explicit
    2.  
    3. Private objMyObject() As MyObject
    4.  
    5. Private Sub Form_Load()
    6.     Dim i As Integer
    7.  
    8.     ReDim objMyObject(1 to 10) As MyObject
    9.  
    10.     For i = 1 To 10
    11.         Set objMyObject(i) = New MyObject
    12.     Next i
    13.  
    14. End Sub
    15.  
    16. Private Sub Form_Unload()
    17.     Erase objMyObject
    18. End Sub

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Erase on array of objects

    I believe Erase is fine, objects are only kept alive whilst they are referenced.

    If you want proof insert a debug.print or some such into the objects Terminate event.

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Erase on array of objects

    Objects have a counter on how many there are, and when the counter reaches zero the object is destroyed. The objects should be properly destroyed when you call Erase, but not all of them get destroyed if there are references to them elsewhere (the internal counter is bigger than 1).

  4. #4

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Erase on array of objects

    Quote Originally Posted by Milk View Post
    I believe Erase is fine, objects are only kept alive whilst they are referenced.

    If you want proof insert a debug.print or some such into the objects Terminate event.
    Oh yeah, not sure why I didn't think of this.

    Quote Originally Posted by Merri View Post
    Objects have a counter on how many there are, and when the counter reaches zero the object is destroyed. The objects should be properly destroyed when you call Erase, but not all of them get destroyed if there are references to them elsewhere (the internal counter is bigger than 1).
    Thanks.

Tags for this Thread

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