Code center > Solved Bugs

Nextelement crash

(1/6) > >>

PurpleYouko:
Here is another one.

Over the weekend, I got a crash in "Nextelement" subroutine in the "mutations" module

--- Code: ---Public Function NextElement(ByRef DNA() As block, beginning As Integer, tipo As Integer, value As Integer) As Integer
  'takes the input for the first value in a gene and returns the position of the next statement
  'as defined by tipo and value
  Dim k As Integer
  Dim uboundarray As Long
  
  uboundarray = UBound(DNA())
  If DNA(uboundarray).tipo <> 4 And DNA(uboundarray).value <> 4 Then
    ReDim Preserve DNA(uboundarray + 1)
    DNA(uboundarray + 1).tipo = 4
    DNA(uboundarray + 1).value = 4
  End If
  k = beginning
  
  If beginning > 0 And beginning < uboundarray Then
    While Not (DNA(k).tipo = 4 And DNA(k).value = 4) And Not (DNA(k).tipo = tipo And DNA(k).value = value)
      k = k + 1
          Wend
    If Not (DNA(k).tipo = tipo And DNA(k).value = value) Then k = -1
  Else 'beginning wasn't valid
    k = -1
  End If
  
  NextElement = k
End Function
--- End code ---

for some reason 'k' was getting bigger than the maximum array size (defined in 'uboundarray')

Below is the fix for it.

By adding a conditional to exit the while loop if 'k' exceeds the allowed value, we can prevent the crash. Not sure if this will cause other problems further down the line by the function returning the wrong value. Shouldn't though because the conditional outside the while loop should set 'nextelement' to -1 anyway.
on jumping out of the while loop, 'k' has to first be set to a usable value so I set it to the maximum possible value of 'uboundarray'.


--- Code: ---Public Function NextElement(ByRef DNA() As block, beginning As Integer, tipo As Integer, value As Integer) As Integer
  'takes the input for the first value in a gene and returns the position of the next statement
  'as defined by tipo and value
  Dim k As Integer
  Dim uboundarray As Long
  
  uboundarray = UBound(DNA())
  If DNA(uboundarray).tipo <> 4 And DNA(uboundarray).value <> 4 Then
    ReDim Preserve DNA(uboundarray + 1)
    DNA(uboundarray + 1).tipo = 4
    DNA(uboundarray + 1).value = 4
  End If
  k = beginning
  
  If beginning > 0 And beginning < uboundarray Then
    While Not (DNA(k).tipo = 4 And DNA(k).value = 4) And Not (DNA(k).tipo = tipo And DNA(k).value = value)
      k = k + 1
      If k > uboundarray Then k=uboundarray GoTo jumpout
    Wend
jumpout:
    If Not (DNA(k).tipo = tipo And DNA(k).value = value) Then k = -1
  Else 'beginning wasn't valid
    k = -1
  End If
  
  NextElement = k
End Function
--- End code ---

Testlund:
I think I got this crash; run-time error 9: out of bonds something, I don't remember.

This line of code was marked with yellow in debug mode:

While Not (DNA(k).tipo = 4 And DNA(k).value = 4) And Not (DNA(k).tipo = tipo And DNA(k).value = value)

I added your fix but there seem to be something wrong with it. This line of code is displayed in red text:

If k > uboundarray Then k=uboundarray GoTo jumpout

I also get a 'Compile error: Expected: End of statement'

The word 'Goto' gets marked.

Testlund:
See for yourself then:

PurpleYouko:
for some reason when I put that code up in a "code" window, it didn't show the colon in the middle of the line.

This is what it should be.

If k > uboundarray Then k=uboundarray: GoTo jumpout

The colon indicates the end of one action and the start of the next.

Testlund:
That didn't fix it either, PY! Did you even try to run it yourself??  :pokey:

Navigation

[0] Message Index

[#] Next page

Go to full version