Weird Array assigments occuring

thedesolatesoul

Expert
Licensed User
Longtime User
Maybe I am missing something obvious but this bug has been doing my head in!

Since I have narrowed it down to between 2 statements, here is my code:

B4X:
      Log ("AddtoPath:" & PathPointIndex & ":" & p.x & "," & P.y & " start:" & PathPoints(0).x & "," & PathPoints(0).y)
      PathPointIndex = PathPointIndex + 1
      SetPoint(PathPoints(PathPointIndex),P.x,P.y)
      Log ("AddtoPath:" & PathPointIndex & ":" & p.x & "," & P.y & " start:" & PathPoints(0).x & "," & PathPoints(0).y)

where SetPoint is:
B4X:
Sub SetPoint(P As PointType,x1 As Int,y1 As Int)
   P.x = x1
   P.y = y1
End Sub

Okay, so the problem is that:
PathPoints(0) is unchanged on the first run of this code (as expected)
but on all subsequent times this part of the code is executed
PathPoints(0) is getting updated to a new value (which is actually PathPoints(1)'s value).

Here is what my log looks like before and after the execution of this statement:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
AddtoPath:0:395,476 start:373,436
AddtoPath:1:395,476 start:373,436
AddtoPath:0:377,547 start:345,513
AddtoPath:1:377,547 start:377,547

Can anyone spot an obvious mistake, but I do not understand why B4A is updating 2 values from the array?

Thanks!
 

margret

Well-Known Member
Licensed User
Longtime User
code issue

Maybe I am missing something obvious but this bug has been doing my head in!

Since I have narrowed it down to between 2 statements, here is my code:

B4X:
      Log ("AddtoPath:" & PathPointIndex & ":" & p.x & "," & P.y & " start:" & PathPoints(0).x & "," & PathPoints(0).y)
      PathPointIndex = PathPointIndex + 1
      SetPoint(PathPoints(PathPointIndex),P.x,P.y)
      Log ("AddtoPath:" & PathPointIndex & ":" & p.x & "," & P.y & " start:" & PathPoints(0).x & "," & PathPoints(0).y)

where SetPoint is:
B4X:
Sub SetPoint(P As PointType,x1 As Int,y1 As Int)
   P.x = x1
   P.y = y1
End Sub

Okay, so the problem is that:
PathPoints(0) is unchanged on the first run of this code (as expected)
but on all subsequent times this part of the code is executed
PathPoints(0) is getting updated to a new value (which is actually PathPoints(1)'s value).

Here is what my log looks like before and after the execution of this statement:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
AddtoPath:0:395,476 start:373,436
AddtoPath:1:395,476 start:373,436
AddtoPath:0:377,547 start:345,513
AddtoPath:1:377,547 start:377,547

Can anyone spot an obvious mistake, but I do not understand why B4A is updating 2 values from the array?

Thanks!

The problem is not in the code you have listed. If its in the activity create that calls the sub, you need to move it out or use if firsttime statement.

MARGRET
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
What is happening is that you are creating references to P in your PathPoints array, you need to re-dim P each time.

Check out this thread for more info.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
kickaha you are spot on. :sign0188:
I needed to ReDim my PathPoints array since they are constantly sent around functions and subs.

But I have a few more questions:
I dont understand how the previous reference still stands.
When I call SetPoint the first time, it returns modifying a non-primitive and returns.
When I call it the second time, it returns modifying a different non-primitive.
How does the second call, make it modify the first non-primitive?

Another question:
So PathPoints is a Global variable in a module. When I ReDim it from a sub, shouldnt that make it a local variable?

And another:
Each time I ReDim the variable, that allocates space in memory? Should I be destroying the previous space somehow?

Thanks alot everyone for replies.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Global Variables

kickaha you are spot on. :sign0188:
Another question:
So PathPoints is a Global variable in a module. When I ReDim it from a sub, shouldnt that make it a local variable?

And another:
Each time I ReDim the variable, that allocates space in memory? Should I be destroying the previous space somehow?

Thanks alot everyone for replies.

I have many sections of code that I have public variables that are re-dimmed in subs and they remain Global and all modules and subs can still access them. I have seen no way to clear the memory from a Dimmed variable. If you find one I would like to know it as well.

Thanks,

Margret
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I seem to remember that if you assign null to a variable in java, the system will automatically reclaim the space if memory starts to run low.

Erel will know if this extends to B4A - and I am sure he will tell us!
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Thanks.
Sorry to nitpick on words, just wanted to make sure I understand this correctly.
When you ReDim an object there is no way to access the old object, but it may still contain some values etc...
So I guess the garbage collector will eventually free it, but does it have any performance impact? I heard if the garbage collector has to do too much work it slows the program down?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sometimes there may be other references to the older object. For example if you use the same variable to add several views to an activity then the "old" views will not be released because the activity holds reference to these views.

In most cases you shouldn't be bothered by creating new objects. The only exception is if you actually see memory / performance issues. Creating many large bitmaps for example will lead to such issues. Smaller objects will usually won't have any effect on performance.
 
Upvote 0
Top