Discussion:
Bug - EXE different to IDE
(too old to reply)
Tony Proctor
2007-10-06 18:47:34 UTC
Permalink
I don't need a solution to this issue folks. I just want to share something
I've found that might be useful one day...

I recently had an EXE that was crashing with error 9 ("Subscript out of
range"), but worked perfectly well in the IDE. I used the VC++ debugger to
pin it down to the code generated for a 'With' block when referencing an
element of an array of UDTs. In the EXE, the array is examined before the
index expression is evaluated. Hence, if the index expression involves a
function call that populates the array then it crashes. In the IDE, the
index expression appears to be evaluated first and so it works OK.

To put this in perspective, here's a very cut-down example. The iLookup()
function lookups up a date+description in the UDT array. If it's not found
then it creates a new entry. In all cases, it returns the index of the
relevant array entry. When the Form_Load is invoked, the 'With
tArray(iLookup(...))' call is suppose to add the associated date+description
arguments to the array, and then print them out. However, tArray has is not
initialised and does not have a SafeArray description associated with it
before the index expression (i.e. the iLookup call) is evaluated. The EXE
therefore fails while the IDE works.

Tony Proctor

'============= Form1 ================
Private Type Item
dDate As Date
sDesc As String
End Type

Private bInit As Boolean
Private tArray() As Item

Private Function iLookup(dDate As Date, sDesc As String)
If bInit Then
For iLookup = LBound(tArray) To UBound(tArray)
' Check if we've already got this date
If tArray(iLookup).dDate = dDate Then Exit For
Next iLookup
If iLookup > UBound(tArray) Then
' Need to add new date
ReDim Preserve tArray(0 To iLookup)
tArray(iLookup).dDate = dDate
tArray(iLookup).sDesc = sDesc
End If
Else
' Add very 1st date
ReDim tArray(0)
iLookup = 0
tArray(iLookup).dDate = dDate
tArray(iLookup).sDesc = sDesc
bInit = True
End If
End Function

Private Sub Form_Load()
With tArray(iLookup(Date, "Today"))
MsgBox .dDate & " " & .sDesc
End With
End Sub

'==================================
Ken Halter
2007-10-07 02:05:27 UTC
Permalink
Post by Tony Proctor
I don't need a solution to this issue folks. I just want to share something
I've found that might be useful one day...
fwiw, it works when compiled to PCode.
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm
Ralph
2007-10-07 03:19:13 UTC
Permalink
Post by Ken Halter
Post by Tony Proctor
I don't need a solution to this issue folks. I just want to share something
I've found that might be useful one day...
fwiw, it works when compiled to PCode.
That's because code "running" in the IDE is PCode.

-ralph

Loading...