Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two WITH statements: error at second WITH when first END WITH not executed #1538

Closed
RZulu54 opened this issue Apr 13, 2023 · 3 comments
Closed

Comments

@RZulu54
Copy link

RZulu54 commented Apr 13, 2023

Describe the bug
Two WITH statements: error at second WITH when first END WITH not executed.
Strange: Not always crashing but at loop count 65534 (internal stack overflow?)

To Reproduce
Steps to reproduce the behavior:

  1. Add Code and button to Form
  2. Click on button
  3. Code to Debug:
    Class Form1

Private Type HashTableEntry
Position1 As Long ' 2x32 bit position hash key
' Position2 As Long
' Depth As Integer ' not Byte, negative values possible for QSearch
' Generation As Byte
' IsChecking As Boolean
' MoveFrom As Byte
' MoveTarget As Byte
' MovePromoted As Byte
' EvalType As Byte
' Eval As Long
' StaticEval As Long
' PvHit As Byte
End Type

Private HashTable() As HashTableEntry

Private Sub Command1_Click()
    ReDim HashTable(272004)
    Test2
    MsgBox "Ready"
End Sub

Private Sub Test2()
    Dim i As Long, j As Long, x As Long
    
    For i = 0 To 272004 - 1 ' <<<< error at 65534
      ' For i = 100000 To 272004 - 1  '<<< error at 165534  > Loop start + 65534
      ' For i = 65534 To 65534 '<<< OK
      ' For i = 64000 To 66000 '<<< OK
       x = 1
        
       ' this works ok ("End With" follows "Next" when EXIT FOR)
       '   With HashTable(i)
       '     For j = 0 To 1
       '         .Position1 = 99
       '        Exit For <<<<< 
       '     Next j
       '   End With
    
      ' this creates the error ("Exit For" jumps behind "End With")
      For j = 0 To 1
        With HashTable(i + j)
            .Position1 = 99
            Exit For ' <<<<< cause for error: jumps over "End With"  
        End With
      Next j
    
      With HashTable(x)   '<<<< error here at i = 65534  ( x always 1)
        .Position1 = 11
      End With
   Next i
End Sub

End Class

  1. See error

Expected behavior
Jump over END WITH should close WITH range too

Screenshots
If applicable, add screenshots to help explain your problem.
grafik

Desktop (please complete the following information):

  • Windows 10
  • twinBASIC compiler version 294

Additional context
Add any other context about the problem here.

@RZulu54
Copy link
Author

RZulu54 commented Apr 14, 2023

Looking deeper in this problem I have no idea how VB6 is handling this problem of deleting the pointer to the first WITH object. But even für large loop numbers (10.000.000) I cannot see virtual memory going up in VB6.
My original WITH code was in a SUB that was called by the main loop. For this case an easy fix would be to remove the WITH references at END SUB.

@WaynePhillipsEA
Copy link
Collaborator

This is being caused by the SAFEARRAY lock. When you enter the With block, the underlying SAFEARRAY lock count (cLocks) is increased using SafeArrayLock() API. On exiting the with block, the lock is automatically removed with the SafeArrayUnlock() API. In this case, the unlock is not occurring due to the jump outside of the block. Although the cLocks field is 32-bit wide, the SafeArrayLock API fails when the lock count reaches &HFFFF (65535).

I'll check, but I believe the behaviour described above is correct (i.e. the lock is correctly still enforced when jumping out of the block), and that the error is actually being caused by the re-entry of the With block not accommodating the already locked state.

@WaynePhillipsEA
Copy link
Collaborator

Fixed in BETA 295, thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants