Results 1 to 40 of 83

Thread: How much do you trust the Collection class?

Threaded View

  1. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: How much do you trust the Collection class?

    Okay, it does seem to be a contiguous block of memory. Here's what I did:

    Code:
    
    Option Explicit
    '
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
    '
    Private Type VbCollection
        pVTable As Long     ' &h0000 
        Unk0    As Long     ' &h0004 
        Unk1    As Long     ' &h0008 
        Unk2    As Long     ' &h000C 
        Count   As Long     ' &h0010 
        Unk3    As Long     ' &h0014 
        First   As Long     ' &h0018 
        Last    As Long     ' &h001C 
    End Type                ' &h0020 length 
    '
    Private Type VbCollectionItem
        Data    As Variant  ' &h0000 
        Key     As String   ' &h0010 
        Prev    As Long     ' &h0014 
        Next    As Long     ' &h0018 
    End Type                ' &h001C length 
    '
    
    Private Sub Form_Load()
        Dim c As New Collection
        Dim p() As Long
        Dim i As Long
        Dim a() As String
        Dim b() As Long
        Dim s As String
    
        ReDim a(1 To 1000)
    
        c.Add "asdf", "asdf"
        c.Add "qwer", "qwer"
        c.Add "cdef", "cdef"
        c.Add "fghj", "fghj"
    
        ' Attempt to allocate/deallocate some memory 
        Erase a
        ReDim b(1 To 5000)
        s = String(3000, "x")
    
        c.Add "cfgh", "cfgh"
        c.Add "aihg", "aihg"
        c.Add "werb", "werb"
        c.Add "bbbb", "bbbb"
        c.Add "ercd", "ercd"
    
    
        p = CollectionNextPointers(c)
        For i = 0 To c.Count
            Debug.Print p(i)
        Next i
    
    End Sub
    
    Public Function CollectionNextPointers(Coll As Collection) As Long()
        ' This returns an array of the entire collection's "Next" pointers. 
        ' The array's zeroth element is the pointer from the header to the first item. 
        Dim CollNextPtrs() As Long
        Dim j As Long
        Dim iHold As Long
        Dim ptr As Long
        '
        If Coll Is Nothing Then
            Err.Raise 91
            Exit Function
        End If
        '
        If Coll.Count = 0 Then Exit Function
        '
        ReDim CollNextPtrs(0 To Coll.Count)
        CopyMemory ptr, ByVal ObjPtr(Coll) + &H18, 4    ' VbCollection.First 
        CollNextPtrs(0) = ptr
        For j = 1 To Coll.Count
            CopyMemory ptr, ByVal ptr + &H18, 4         ' VbCollectionItem.Next 
            CollNextPtrs(j) = ptr
        Next j
        '
        CollectionNextPointers = CollNextPtrs
    End Function
    
    
    Particularly notice the line in between adding the list of items. It would seem that that would certainly shuffle memory around.

    But here's my new list of "first/next" pointers.

    Code:
    
     107868824
     107868912
     107869000
     107869088
     107869176
     107869264
     107869352
     107869440
     107869528
     0
    
    
    Contiguous blocks of 88 (&h58) bytes each.

    Actually, this brings up yet another question. From what you've (DEX) posted, we only know about &h1C bytes in the VBCOLLECTIONITEM structure. Where/what are the other 60 (&h3C) bytes?

    Regards,
    Elroy
    Last edited by Elroy; Aug 26th, 2016 at 04:23 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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