Results 1 to 40 of 83

Thread: How much do you trust the Collection class?

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Posts
    70

    How much do you trust the Collection class?

    Let's face it: VB's Collection is buggy. At least on my machine under Windows 8, using VB6 Professional. You're welcome to check, if this is also the case for yourself, on your platform.

    Here's how I can reproducibly enforce an error on my platform.

    (1) Create a dynamic 0-based string array with n elements (0...n-1).
    (2) Create a new collection.
    (3) Fill the string array with random 96 bit sequences by concatenating 6 Unicode characters of: ChrW(Int(Rnd() * 65535))
    (4) Add into the collection all n elements and give as key the random 96 bit sequences stored in the string array.
    (5) Retrieve all n elements from the collection by key as per the string array.

    Here's how to reproduce the error:

    (1) Let the number of items be n=13366.
    (2) Run the shown code in the IDE. The final MsgBox will pop up and say "Done".
    (3) Compile into an exe.
    (4) Run the compilation. The final MsgBox will pop up and say "Done".

    So, all is well. Or isn't it?

    (1) Let the number of items now be n=13367 (one more than before).
    (2) Run the shown code in the IDE. The final MsgBox will pop up and say "Done".
    (3) Compile into an exe.
    (4) Run the compilation. Runtime error 5 will be raised.

    Obviously, the 13367th item (i=13366) causes a problem.

    To find out, what element triggers the RTE, uncomment the 3 commented lines at position 1 in the Main routine. They will produce a log file on your desktop, first writing the key written last, then logging what is attempted to be retrieved, and after successful retrieval reporting the success. My log file looks like this:
    Code:
     13366        Error cause adding this item: 0736 1196 B6D2 EA09 97A0 A928 
     0            Attempt to access:            B49E 888D 945A 4A20 4D4C C654 - Success
     1            Attempt to access:            0396 C2BE D081 B582 0B9C 69FD - Success
     2            Attempt to access:            DCD3 CA5C 5F9F F641 DF16 0E65 - Success
     ...
     4215         Attempt to access:            CC37 472A E84D BEEE 3CE7 2D6B - Success
     4216         Attempt to access:            CC9C 8CD3 A2C9 CD14 B9AE A16B - Success
     4217         Attempt to access:            1192 BB0B 80D9 D8BA 0534 5374 -
    No success on item 4217 (the 4218th item), here a run time error 5 is raised. But in the compilation only, in the IDE all is well.

    Here's the code to check if you can reproduce this error also on your platform.

    In the LOGFILE constant, you need to replace ... by the user name to point your desktop directory. (Right-click on any item on your desktop and choose Properties to find the location). The code will then produce a text file named Log.txt on your desktop, which you can open with any editor.

    Enjoy.

    Code:
    Option Explicit
    
    Private Const ITEMS As Long = 13367
    'Adding 13367+ items (i=0...13366+) as below produces an access error in the
    '4218th element (i=4217), adding less items grants access to all of them.
    
    'This is element i=13366: 0x0736 1196 B6D2 EA09 97A0 A928
    'This is element i= 4217: 0x1192 BB0B 80D9 D8BA 0534 5374
    
    Private Const LOGFILE As String = "C:\Users\...\Desktop\Log.txt"
    
    Private oColl As Collection
    Private i As Long, j As Long
    Private sKey As String
    Private asKeys() As String
    
    Sub Main()
        ReDim asKeys(0 To ITEMS - 1) As String
        
        Set oColl = New Collection
        
        'Generating ITEMS keys.
        For i = 0 To ITEMS - 1
            For j = 1 To 6
                asKeys(i) = asKeys(i) & ChrW(Int(Rnd() * 65535))
            Next j
        Next i
        
        'Adding ITEMS key into collection.
        For i = 0 To ITEMS - 1
    '       If i = 13366 Then LogCause          'Log error cause item i = 13366.
            oColl.Add i, asKeys(i)
        Next i
        
        'Retrieving all keys from collection.
        For i = 0 To ITEMS - 1
    '       LogStart                            'Log access attempt.
            oColl.Item (asKeys(i))              'Attempt to access.
    '       LogSuccess                          'Log successful attempt.
        Next i
        
        Set oColl = Nothing
        
        MsgBox "Done"
    End Sub
    
    Sub LogCause()
        Open LOGFILE For Append As #1
            Print #1, i, "Error cause adding this item: ";
            For j = 1 To 6
                Print #1, Right("000" & Hex$(AscW(Mid$(asKeys(i), j, 1))), 4); " ";
            Next j
            Print #1,
        Close 1
    End Sub
    
    Sub LogStart()
        Open LOGFILE For Append As #1
            Print #1, i, "Attempt to access:            ";
            For j = 1 To 6
                Print #1, Right("000" & Hex$(AscW(Mid$(asKeys(i), j, 1))), 4); " ";
            Next j
            Print #1, "- ";
        Close 1
    End Sub
    
    Sub LogSuccess()
        Open LOGFILE For Append As #1
            Print #1, "Success"
        Close 1
    End Sub
    Last edited by Donar; Aug 25th, 2016 at 12:05 AM.

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