Announcement

Collapse
No announcement yet.

Win crash due to going out of array bounds

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Win crash due to going out of array bounds

    If i have $ERROR BOUNDS ON (or $ERROR ALL ON) in my PB/DOS code, and Windows crashes when running it, without any PB error occurred, may i exclude that the cause of the crash is that i went out of array bounds, or i better assume that Windows might crash before allowing PB to trap the error ?

    ------------------
    Davide Vecchi
    [email protected]

  • #2
    Just because you compile with $ERROR BOUNDS ON does not mean errors will be interecepted in your program. You need to be running with an 'ON ERROR GOTO whatever' statement, too.

    Array bound violations are a good place to look, but no necessarily the cause of your problem.

    Add an error trap to your program; in that procedure, report ERR and ERADR, then run ERADR through Compile/Find Error to locate the offending line of source code.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      You don't have to use an error trap, but your code should be checking for errors in it's execution or you may face unexpected results when an array subscript access fails unexpectedly.

      Other than array errors, be sure to look for bad pointer and indexed-pointer usage, inline assembler code problems, problems in PBU modules that were not compiled with [bounds] error testing enabled, etc.

      The first test it to add $ERROR ALL ON to the code and retry it.

      Essentially, you'll need to narrow down the place the app fails. Note that this may not be the place where the problem is being introduced, but rathert could just be the location that the problem comes to light.

      For example, you could be introducing a memory corruption in one part of your code (say, overwriting the wrong memory locations), but the effect of this mis-write may not show up until the affected memory is used sometime later in your code.



      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        You need to be running with an 'ON ERROR GOTO whatever' statement
        Sorry, rather than “trapping” the error, i had to talk about raising the error; it wasn’ t about error trapping.

        That is, when one has $ERROR BOUNDS ON and violates array bounds, is it possible that Windows crashes before PB can raise the Error 9 (Subscript out of range) ?

        Lance, thanks for the suggestions, i’ ll keep them in mind. However, actually i’ m not facing any real Windows crash. It’ s only that i was wondering if i would have made my sw better debuggable by accessing arrays through a function that assures that any bounds violation is caught before causing any unclear crash.
        Something like this (only for the accesses that aren’ t too much time-critical):

        Code:
        FUNCTION INTArrayElement(MyArray() AS INTEGER, Index AS LONG) AS INTEGER
        
        ‘ For monodimensional integer arrays only.
        
        	IF Index >= LBOUND(MyArray) AND Index <= UBOUND(MyArray) THEN
        
        		FUNCTION = MyArray(Index)
        
        	ELSE
        		PRINT “Congratulations, you tried to access array index”; Index; “which is out of bounds.”
        		END
        
        	END IF
        
        END FUNCTION
        and instead of, for example,

        A = Prices(n)

        using

        A = INTArrayElement(Prices(), n)

        Sure it would be better writing code that doesn’ t violate array bounds, but...

        ------------------
        Davide Vecchi
        [email protected]

        Comment


        • #5
          $ERROR BOUNDS ON works basically like this:
          Code:
          IF Index >= LBOUND(arr) AND Index <= UBOUND(arr) THEN
            ... do the operation
          ELSE
            ERROR 9
          END IF
          So, basically, if the bounds test fails, the array subscript is not referenced. Therefore, we can gather that $ERROR BOUNDS ON works preemptively, rather than reactively.

          Or to put it another way, $ERROR BOUNDS ON will protect you from yourself, but whether your code can detect and react "properly" to a violation notification (Error 9) in the context of your application design is another matter altogether.

          Going back to your original question, if you are using $ERROR BOUNDS ON, and Windows crashes when your app runs, you can be pretty sure it was not an invalid array subscript problem, but something else.

          However, lets say somthing else in your code overwrites the array descriptor table and damages the LBOUND/UBOUND info... in that case, subsequent code that relies on $ERROR BOUNDS testing may not produce usable results and reliably detect errors.

          Such is the problem with subtle memory corruption caused by programmer error.

          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            Very clear, that was exactely my question; so, as long as i have $ERROR BOUNDS ON, checking the subscript before accessing the array is s uselesss.
            Thanks.

            ------------------
            Davide Vecchi
            [email protected]

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎