Bug? Corrupted array values

Simon Kinsey v2

Member
Licensed User
Longtime 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.
 

Attachments

  • easter game.zip
    5.9 KB · Views: 296
  • Easter files.zip
    222.3 KB · Views: 264

Simon Kinsey v2

Member
Licensed User
Longtime 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:
B4X:
    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.
 

Simon Kinsey v2

Member
Licensed User
Longtime User
However, the following does work:

B4X:
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?
 

Simon Kinsey v2

Member
Licensed User
Longtime 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.
 
Top