Bug? Corrupted array values

Discussion in 'B4J Bugs & Wishlist' started by Simon Kinsey v2, Feb 2, 2015.

  1. Simon Kinsey v2

    Simon Kinsey v2 Member Licensed User

    I seem to be having problems with this easter egg game, that I think is due to an array of egg objects getting corrupted. The game works fine, until the values in the array are reset to their original values with the reset button. The code gives an error message when array item (0,0) changes from its initial value, set up in a nested loop. The error occurs in the Egg_stack module.

    The code is modified from a prototype I wrote and tested in C, so I know that this part of the program works OK when compiled in C.

    I have attached the source code.
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Simon Kinsey v2

    Simon Kinsey v2 Member Licensed User

    No, this isn't a problem of 'aliasing', where a variable refers to an object rather than being an object. The only code where the array of objects changes value is here:
    Code:
    For n=0 To colourCount - 1
            
    For m=0 To EGGSEND
                
    Dim c, t As Int
                t =  {calculated value
    }
                c =  {calculated value}
                stack(n,m).Initialize(t, c)
                  next
    next
    where {calculated value} is a function that calcultes the value from a string, not relevant to this problem.

    Stepping through the code, initially, stack(0,0) has the correct value of (0,0). However, sometimes, when n=1 and m=0, stack(0,0) returns to its previous value of (1,2). This is not a consistent problem. At other times I have run the code and stack(0,0) keeps its value. It depends on whether the player of the game got very far in the game before restarting it.
     
  4. Simon Kinsey v2

    Simon Kinsey v2 Member Licensed User

    However, the following does work:

    Code:
    For n=0 To colourCount - 1
            
    For m=0 To EGGSEND
                
    Dim c, t As Int
                
    Dim e as Egg
                t =  {calculated value
    }
                c =  {calculated value}
               e.Initialize(t,c)
                stack(n,m) = e
                  next
    next
    so it seems that when an object is initialized, the previous values are not properly destroyed beforehand. Is this right?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    This is actually the same issue as the one I was referring.

    Only when you call Dim e As Egg you are creating a new instance.
     
  6. Simon Kinsey v2

    Simon Kinsey v2 Member Licensed User

    So you cannot re-initialise the same instance to new values? OK, thank you Erel.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can. But you aren't creating a new instance so all the references still point to the same single object.
     
  8. Simon Kinsey v2

    Simon Kinsey v2 Member Licensed User

    Yes, Erel, I see you are right. The flaw in my code is that in C I was able to move data structures - eggs - where in Java and B4J I move references to objects. When the game restarts I only re-instantiate some of the array elements, and these are now pointing to different objects. There is no bug.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice