Menu

#115 Prevent redimming of arrays when elements are in use.

open
nobody
None
2012-10-09
2007-01-27
Matthew
No

If you redim an array while an element of it is used in a With block, or passed by reference as a parameter, then there is a good chance the element won't be where you expect next time you try to access it.

In order to help prevent this, would it be possible to lock arrays in this situation so that (at least in -exx mode) it will alert you when this happens?

I wouldn't bother asking, since it's probably quite tricky to do, but errors of this ilk can be hard to track down.
Thanks.

Discussion

  • lOOOlOl

    lOOOlOl - 2007-03-14

    Logged In: YES
    user_id=1405699
    Originator: NO

    Personally I tend to pass arrays as parameters for the explicit purpose of resizing them and, in general, array management. This is more of a "code better" issue then anything else. However, ArrayLock" and "ArrayUnlock" functions would be handy to help manage these issues. If an array is locked then trying to redim/erase it should generate an error.

     
  • Matthew

    Matthew - 2007-03-14

    Logged In: YES
    user_id=723706
    Originator: YES

    Yeah, it can be useful to pass arrays as parameters for things like resizing, but that would cause unexpected effects if elements are in use somewhere on the stack.
    I like the idea of specific lock/unlock functions. It sounds like it would (hopefully) be a lot easier to add.
    But to be honest, I don't think it will solve the problem as effectively. To be safe, it involves locking/unlocking the array every time you pass an element somewhere, which is hard to remember and adds to the code.

     
  • Ruben

    Ruben - 2007-08-10

    Logged In: YES
    user_id=1399002
    Originator: NO

    I suppose this is sort of the same class of problem as resizing a shared array of type FOO while calling a method using one of those FOO objects in the array. Perhaps there is a practical solution, but I think it's just one of those things that has to be documented...

    I can't really think how a lock would work since we're talking about changing addresses the 'redim' function knows nothing about.

     
  • Matthew

    Matthew - 2007-08-11

    Logged In: YES
    user_id=723706
    Originator: YES

    Hi, thanks for responding here.
    Well there's one way I can think of to do it, but I don't know how practical it would be, or how much work it would take.
    Basically, whenever you pass an array element as a parameter to a function, you lock the array. After the function completes, you unlock it again. Similar principle for "with" blocks:

    array1.lock
    foo( array1(i) )
    array1.unlock

    To allow for nested locking, I guess that could be done with a lock counter on the array descriptor. Unlocked arrays would have a counter of 0.

    array1.lockcnt += 1
    with array1(i)
    end with
    array1.lockcnt -= 1

    Obviously locks can't realistically be used if you start using pointers instead of references, so in those cases you're pretty much on your own. But it should work if you're just passing references to elements, since for those cases FB is always able to tell the array when it should/shouldn't be locked.

     

Log in to post a comment.